search

Home  >  Q&A  >  body text

objective-c - RAC: incompatible block pointer types sending

ps: I am using 'ReactiveObjC' ~> '2.1.0'
After changing the parameter of subscribeNext to UIColor, I get the error "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) {
    
}];

But when I use the temporary variable validSearchTextSignal to store the signal and then subscribeNext, it works, as shown below, the compilation passes and runs normally

// 方法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;
}];

In fact, the modification method is very simple. In addition to the above method, you can also perform type conversion on the NSString variable in subscribeNext's Block. But I'm curious about the difference between method 1 and method 2. Why can method 2 be implemented, but method 1 cannot be compiled.
I checked the declaration of this method and the pointed type is id type, but why does the compiler prompt the NSString type in the method 1 scenario. In the method 2 scenario, the prompt is the id type. The following is the declaration of the method. The display type is the id type, and no method declaration of the NSString type is found

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

Supplementary information:
There is a similar question on stackoverflow RACSignal: Handling incompatible block pointer types, one answer is "The reason for the error is because subscribeNext block returns void and by placing a return will generate the incompatibility with the block signature.” I still don’t understand why

过去多啦不再A梦过去多啦不再A梦2787 days ago1426

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-02 09:36:46

    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

    reply
    0
  • Cancelreply