你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

three20代码之NSObjectAdditions

2022/5/17 6:40:35

扩展了performSelector,使其可以获取返回值和带更多的参数。

@implementation NSObject (TTAdditions)



///
- (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3 {
  NSMethodSignature *sig = [self methodSignatureForSelector:selector];
  if (sig) {
    NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
    [invo setTarget:self];
    [invo setSelector:selector];
    [invo setArgument:&p1 atIndex:2];
    [invo setArgument:&p2 atIndex:3];
    [invo setArgument:&p3 atIndex:4];
    [invo invoke];
    if (sig.methodReturnLength) {
      id anObject;
      [invo getReturnValue:&anObject];
      return anObject;

    } else {
      return nil;
    }

  } else {
    return nil;
  }
}

@end