更加灵活的“ performSelector

I want to invoke a selector of a method that has the usual NSError** argument:

-(int) getItemsSince:(NSDate *)when dataSelector:(SEL)getDataSelector error:(NSError**)outError  {
    
NSArray *data = nil;
    
if([service respondsToSelector:getDataSelector]) {
        data 
= [service performSelector:getDataSelector withObject:whenwithObject:outError];
        
// etc.

... which the compiler doesn't like:

warning: passing argument 3 of 'performSelector:withObject:withObject:' fromincompatible pointer type

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
签出 NSInvocation,这使您可以更灵活的方式的"performSelector"。
if ([service respondsToSelector:getDataSelector]) {
    
NSArray *data;
    
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
        
[service methodSignatureForSelector:getDataSelector]];
    
[invocation setTarget:delegate];
    
[invocation setSelector:getDataSelector];
    
// Note: Indexes 0 and 1 correspond to the implicit arguments self and _cmd, 
    
// which are set using setTarget and setSelector.
    
[invocation setArgument:when atIndex:2]; 
    
[invocation setArgument:outError atIndex:3];
    
[invocation invoke];
    
[invocation getReturnValue:&data];
}
////////////////////////////////////////////
perform selector调用超过两个以上参数的方法
也可以把所有的参数放到字典里, 像NSNotification或者NSTimer的userinfo

你可能感兴趣的:(selector)