@selector - With Multiple Arguments?

前端 未结 9 730
刺人心
刺人心 2020-12-13 02:26

I have been using @selector today for the first time and have not been able to work out how to do the following? How would you write the @selector

相关标签:
9条回答
  • 2020-12-13 03:09

    Using NSInvocation as you specify you can create an NSObject category that implements

    - (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments;
    

    Something like:

    - (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments
    {
        NSMethodSignature *signature = [self methodSignatureForSelector: aSelector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
        [invocation setSelector: aSelector];
    
        int index = 2; //
        for (NSObject *argument in arguments) {
            [invocation setArgument: &argument atIndex: index];
            index ++;
        }
        [invocation invokeWithTarget: self];
    }
    

    from: iOS - How to implement a performSelector with multiple arguments and with afterDelay?

    0 讨论(0)
  • 2020-12-13 03:10

    Another option is to use an even shorter syntax:

    #import <objc/message.h> // objc_msgSend
    ...
    ((void (*)(id, SEL, Type1, Type2, Type3))objc_msgSend)(target, a_selector, obj1, obj_of_type2, obj_of_type3);
    
    0 讨论(0)
  • 2020-12-13 03:16
    @selector(printText:andMore:)
    
    0 讨论(0)
提交回复
热议问题