objective-c - RAC: incompatible block pointer types sending
过去多啦不再A梦
过去多啦不再A梦 2017-05-02 09:34:46
0
1
1318

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则可以,如下所示,编译通过并正常运行

// 方法2 RACSignal *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.”, 感觉还是没有懂为什么

过去多啦不再A梦
过去多啦不再A梦

reply all (1)
習慣沉默

I am currently using version 2.5, which is relatively stable. I tried to install version 2.1, but every time it was installed 2.1.8, so I can’t see the specific source code. You’d better upgrade, it’s more convenient

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!