美文网首页
iOS NSTimer scheduledTimerWithT

iOS NSTimer scheduledTimerWithT

作者: SmallWhiteMouse | 来源:发表于2018-04-10 16:27 被阅读29次

第一步:配置NSInvocation

- (id)performSelector:(SEL)aSelector withArgument:(id)anArgument{

    if (aSelector == nil) return nil;

    NSMethodSignature  *signature = [[self class] instanceMethodSignatureForSelector:aSelector];

    NSInvocation  *invocation = [NSInvocation  invocationWithMethodSignature:signature];

    invocation.target =self;

    invocation.selector = aSelector;

    //NSInvocation 有两个隐藏参数,所以anArgument

    if ([anArgument isKindOfClass:[NSArray class]]) {

        NSInteger  count = MIN(signature.numberOfArguments-2, [(NSArray *)anArgument count]);

        for (int i = 0; i< count; i++) {

            const char*type  = [signature getArgumentTypeAtIndex:i +2];

            //需要做参数类型判断然后解析成对应类型,这里默认所有参数都是OC对象

            if (strcmp(type, "@") == 0) {

                id argument = anArgument[i];

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

            }

        }

    }

    [invocation invoke];

    return invocation;

}

####如果您有什么疑问或者书写歧义,非常感激您能留言~

相关文章

网友评论

      本文标题:iOS NSTimer scheduledTimerWithT

      本文链接:https://www.haomeiwen.com/subject/bbjnhftx.html