See Also: NSObject Members
RemarksOverview
NSObject is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.
Selectors
NSObject has some special methods that take advantage of the Objective-C runtime system. For example, you can ask a class or instance if it responds to a message before invoking a particular method. You can also ask for a method implementation and invoke it using one of the perform... methods, or as a function, although this is typically discouraged since it circumvents dynamic binding.
These and other NSObject methods take a selector of type SEL as an argument. For efficiency, full ASCII names are not used to represent methods in compiled code. Instead the compiler uses a unique identifier to represent a method at runtime called a selector. A selector for a method name is obtained using the @selector() directive:
SEL method = @selector(isEqual:);The instanceMethodForSelector: class method and the methodForSelector: instance method return a method implementation of type IMP. IMP is defined as a pointer to a function that returns an id and takes a variable number of arguments (in addition to the two “hidden” arguments—self and _cmd—that are passed to every method implementation):
typedef id (*IMP)(id, SEL, ...);This definition serves as a prototype for the function pointer returned by these methods. It’s sufficient for methods that return an object and take object arguments. However, if the selector takes different argument types or returns anything but an id, its function counterpart will be inadequately prototyped. Lacking a prototype, the compiler will promote floats to doubles and chars to ints, which the implementation won’t expect. It will therefore behave differently (and erroneously) when performed as a method.
To remedy this situation, it’s necessary to provide your own prototype. In the example below, the declaration of the test variable serves to prototype the implementation of the isEqual: method. test is defined as a pointer to a function that returns a BOOL and takes an id argument (in addition to the two “hidden” arguments). The value returned by methodForSelector: is then similarly cast to be a pointer to this same function type:
BOOL (*test)(id, SEL, id); test = (BOOL (*)(id, SEL, id))[target methodForSelector:@selector(isEqual:)];while ( !test(target, @selector(isEqual:), someObject) ) { ... }In some cases, it might be clearer to define a type (similar to IMP) that can be used both for declaring the variable and for casting the function pointer methodForSelector: returns. The example below defines the EqualIMP type for just this purpose:
typedef BOOL (*EqualIMP)(id, SEL, id); EqualIMP test; test = (EqualIMP)[target methodForSelector:@selector(isEqual:)];while ( !test(target, @selector(isEqual:), someObject) ) { ... }Either way, it’s important to cast the return value of methodForSelector: to the appropriate function type. It’s not sufficient to simply call the function returned by methodForSelector: and cast the result of that call to the desired type. Doing so can result in errors.
See Messaging in Objective-C Runtime Programming Guide for more information.
RequirementsNamespace: MonoTouch.Foundation
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0