Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 1. Objective-C Pocket Reference > Objective-C++ - Pg. 121

+instanceMethodForSelector : (for subclasses of NSObject ) on the class of the object whose method you will call. Since these methods are declared to return an IMP , which is a pointer to a function whose signature is (id , SEL, ...) you need to cast to your own type the value it returns, to prevent compiler warnings at line 5. Line 5. Use the function pointer as in C. Pass in the receiver and selector as the first two arguments. After you execute lines 1­3, line 5 will have the same effect as calling [obj methodName : anInt] , but will execute faster. Typically this line will be in a loop. Optimizing a method call in this manner only makes sense when you are going to invoke the method repeatedly, such as within a loop. Lines 1 through 3 represent a one-time setup that should be done prior to entering the loop. Line 5 is the optimized method call that would be used inside the loop. Objective-C++ gcc is at once a compiler for C, Objective-C, and C++. You can intermix C++ and Objective-C code to some degree. To instruct the compiler that a file contains C++ code as well as Objective-C, use the file extension .mm or .M instead of .m. Following are some ways in which C++ and Objective-C code can be used together: · Objective-C objects can have fields that point to C++ objects, and vice versa. · Objective-C code can call methods on C++ objects, and vice versa. · Objective-C objects can have C++ objects (as opposed to pointers) as fields, but only if the C++ class has no vir- tual methods. Objective-C++ | 113