RacLiftSelector

// 支持任意参数类型。c语言的基础数据类型;objective-c的 id对象类型。
SEL sel =@selector(bb3:float2:char:);
NSMethodSignature *signature = [self methodSignatureForSelector:sel ];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature ];
invocation.selector = sel   ;
invocation.rac_argumentsTuple = RACTuplePack(@"12345",@(1.2),@"12122");
[invocation invokeWithTarget:self];
NSNumber*a= [invocation rac_returnValue];
NSLog(@"%@", a);

//因为有基础数据类型。  所以这里会crash
self.block11 = ^int(int int1, float float2, char *char3) {
    return 1;
};
NSNumber *(^block22)(NSNumber*n1 , NSNumber*n2, NSString *str3) = ^(NSNumber*n1 , NSNumber*n2, NSString *str3){
    return @(11);
};
//因为有基础数据类型。  所以这里会crash
// [RACBlockTrampoline invokeBlock:self.block11  withArguments:RACTuplePack(@"12345",@(1.2),@"12122")];


[RACBlockTrampoline invokeBlock:block22   withArguments:RACTuplePack(@"12345",@(1.2),@"12122")];

RACBlockTrampoline的参数是ractuple, 原祖的元素类型都是id类型的

  • (id)invokeWithArguments:(RACTuple *)arguments {
    SEL selector = [self selectorForArgumentCount:arguments.count];

    // 方法
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
    // Sel
    invocation.selector = selector;
    // 给谁发送消息
    invocation.target = self;

    for (NSUInteger i = 0; i < arguments.count; i++) {
    id arg = arguments[i];
    NSInteger argIndex = (NSInteger)(i + 2);
    // 参数 --- 仅支持id类型
    [invocation setArgument:&arg atIndex:argIndex];
    }

    [invocation invoke];

    __unsafe_unretained id returnVal;
    [invocation getReturnValue:&returnVal];
    return returnVal;
    }

  • (RACSignal *)rac_liftSelector:(SEL)selector withSignalOfArguments:(RACSignal *)arguments {
    NSCParameterAssert(selector != NULL);
    NSCParameterAssert(arguments != nil);

    @unsafeify(self);

    NSMethodSignature *methodSignature = [self methodSignatureForSelector:selector];
    NSCAssert(methodSignature != nil, @"%@ does not respond to %@", self, NSStringFromSelector(selector));

    return [[[[arguments
    takeUntil:self.rac_willDeallocSignal]
    map:^(RACTuple *arguments) {
    @strongify(self);
    // 方法

          NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
              //Sel
          invocation.selector = selector;
              //参数-----  支持基本数据类型char,int, short,long,long long,
              unsigned char,unsignd int, unsigned short,unsigned long,  unsigned long long
              float, double, BOOL, char *, void (^block)(void) 
          invocation.rac_argumentsTuple = arguments;
              // 给谁发消息  
          [invocation invokeWithTarget:self];
          
          return invocation.rac_returnValue;
      }]
      replayLast]
      setNameWithFormat:@"%@ -rac_liftSelector: %s withSignalsOfArguments: %@", RACDescription(self), sel_getName(selector), arguments];
    

}

你可能感兴趣的:(RacLiftSelector)