zoukankan      html  css  js  c++  java
  • performSelector支持多参数

    默认的performSelector支持最多传递两个参数,要想传递超过两个的参数,需要使用NSInvocation来模拟performSelector的行为,如下:

    - (id)performSelector:(SEL)aSelector withObjects:(NSArray *)argumentsArray

    {

        NSMethodSignature *sig = [selfmethodSignatureForSelector:aSelector];

        if (sig)

        {

            NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:sig];

            [invocation setTarget:self];

            [invocation setSelector:aSelector];

            

            for (int i = 0; i < argumentsArray.count ; ++i)

            {

                id argument = [argumentsArray objectAtIndex:i];

                //此处+2不能漏

                [invocation setArgument:&argument atIndex:i + 2];

            }

            

            [invocation invoke];

            if (sig.methodReturnLength)

            {

                id anObject = nil;

                [invocation getReturnValue:anObject];

                return anObject;

            }

            

            return nil;

        }

        

        returnnil;

    }

  • 相关阅读:
    交换变量方法总结
    Java Object Model(一)
    Git命令文本手册
    移动端浏览器touch事件的研究总结
    jQuery中的end()方法
    AngularJs开发——控制器间的通信
    网站收藏
    HTTP Content-type 对照表
    ViewData和ViewBag的那些事
    jQuery选择器
  • 原文地址:https://www.cnblogs.com/lexingyu/p/3387323.html
Copyright © 2011-2022 走看看