http://rypress.com/tutorials/objective-c/methods.html#selectors
Selectors are Objective-C’s internal representation of a method name. They let you treat a method as an independent entity, enabling you to separate an action from the object that needs to perform it. This is the basis of the Target-Action design pattern, and it is an integral part of Objective-C’s dynamic typing system.
There are two ways to get the selector for a method name. The
@selector() directive lets you convert a source-code method name to a selector, and the NSSelectorFromString() function lets you convert a string to a selector (the latter is not as efficient). Both of these return a special data type for selectors called SEL. You can use SEL the exact same way as BOOL, int, or any other data type.To check whether an object has implement the method you are about to call, use
respondsToSelector e.g.[
if([objectA respondsToSelector:@(methodA){
[objectA performSelector:@(methodA) ];
}
To call a method with parameters: (Only object is allowed as parameter and up to 2 max.)
[objectA performSelector:@(methodB:)
withObject: [NSNumber x:8] ];
沒有留言:
張貼留言