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

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梦

répondre à tous(1)
習慣沉默

J'utilise actuellement la version 2.5, qui est relativement stable. J'ai essayé d'installer la version 2.1, mais à chaque fois que j'installe la version 2.1.8, je ne vois donc pas le code source spécifique. c'est plus pratique

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!