【问题标题】:Getting incompatible block pointer types sending void *(^)获取不兼容的块指针类型发送 void *(^)
【发布时间】:2012-09-23 03:39:44
【问题描述】:
[AsyncRequest performGetRequestWithUrl:[NSString stringWithFormat:@"http://%@/api/streams/%d", @"server.herokuapp.com", userId]
                     completionHandler:^(NSDictionary *result, NSError *error) {
    // Create new SBJSON parser object
    NSError *e;
    NSArray *jsonArray =[NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error: &e];

    NSLog(@"parse result to JSON object with jsonArray: %@ and error: %@", jsonArray, e.description);

    if ([jsonArray valueForKey:@"error"]) {
        return nil;
    }

    NSLog(@"getStreams size of the return array: %d", [jsonArray count]);
    NSMutableArray* data = [[NSMutableArray alloc] initWithCapacity:0];

    if (jsonArray) {
        data = [[NSMutableArray alloc] initWithCapacity:[jsonArray count]];
        for (NSDictionary *item in jsonArray) {
            NSLog(@"item: %@", item);
            [data addObject:[[Stream alloc] initWithJSONObject:item]];
        }
    }

    onComplete(data, error);

}];

我在这段代码中遇到了奇怪的错误。它显示错误消息“获取不兼容的块指针类型将 void *(^)(NSDictionary *_strong, NSError *_strong) 发送到 'void (^)(NSDictionary *_strong, NSError *_strong)'类型的参数'

这是函数签名:

+(void)performGetRequestWithUrl:(NSString *)requestUrl completionHandler:(void (^)(NSDictionary *result, NSError *error))completionBlock

【问题讨论】:

    标签: iphone ios5 objective-c-blocks


    【解决方案1】:

    将“return nil”改为“return”

    【讨论】:

    • @flashsnake 错误消息是说您正在将一个返回 void * 的块传递给一个期望返回 void 的块的参数。在 C 和 Objective-C 中,void * 是任意指针的类型; nil 实际上被定义为void *(0),因此当您输入return nil 时,编译器会推断您的返回类型为void *。而如果您在 return 语句之后什么都没有,或者没有 return,它会推断出 void 的返回类型,正如参数所期望的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2020-07-05
    • 2021-05-27
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多