objective-c - RAC: incompatible block pointer types sending

浏览:65日期:2023-11-20

问题描述

ps:使用的是’ReactiveObjC’ ~> ’2.1.0’将subscribeNext的参数修改为UIColor后报错'incompatible block pointer types sending ’void(^)(UIColor __strong)’ to parameter of type’void(^_Nonnull)(NSString _Nullable __strong)’'

// 默认[[self.searchText.rac_textSignal map:^id(NSString *text) { return [self isValidSearchText:text] ? [UIColor clearColor] : [UIColor yellowColor];}]subscribeNext:^(NSString * _Nullable x) { }];// 方法1 将subscribeNext中的类型修改为UIColor类型[[self.searchText.rac_textSignal map:^id(NSString *text) { return [self isValidSearchText:text] ? [UIColor clearColor] : [UIColor yellowColor];}] subscribeNext:^(UIColor *color) { }];

但当我使用临时变量validSearchTextSignal存储信号再进行subscribeNext则可以,如下所示,编译通过并正常运行

// 方法2RACSignal *validSearchTextSignal = [self.searchText.rac_textSignal map:^id(NSString *text) { return [self isValidSearchText:text] ? [UIColor clearColor] : [UIColor yellowColor];}];[validSearchTextSignal subscribeNext:^(UIColor *color) { self.searchText.backgroundColor = color;}];

其实修改方式很简单,除了上述方法,还可以对suscribeNext的Block中的NSString变量进行类型转换即可。但我比较好奇方法1与方法2的区别在哪里。为什么方法2可以实现,但用方法1就编译不过去。我查看了该方法的声明,指向的类型为id类型,但为什么编译器在方法1情景下提示的是NSString 类型。方法2情景下提示的是id类型。如下为该方法的声明,显示类型为id类型,并没有发现NSString类型的方法声明

- (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock { NSCParameterAssert(nextBlock != NULL);RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock error:NULL completed:NULL]; return [self subscribe:o];}

补充资料:stackoverflow上也有类似的问题RACSignal: Handling incompatible block pointer types, 有一个回答为“The reason for the error is because subscribeNext block returns void and by placing a return will generate the incompatibility with the block signature.”, 感觉还是没有懂为什么

问题解答

回答1:

我现在使用的是2.5版本的, 比较稳定, 我尝试安装2.1版本, 但是每次都是安装的2.1.8, 所以看不到具体的源码, 你最好还是升级下吧, 方便些

相关文章: