iOS - AFNetworking GET 方法中不兼容的块指针类型

iOS - Incompatible block pointer types in AFNetworking GET method

我正在学习使用 AFNetworking 并执行 GET 请求。

AFNetworking (3.1.0)

XCode 6

代码:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://localhost:5000/index" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error)p;
    }];

但是,我得到了错误

Incompatible block pointer types sending 'void (^)(NSURLSessionTask *__strong, NSError *__strong)' to parameter of type 'void (^ __nullable)(NSURLSessionDataTask * __nonnull __strong)'

试了很多次都不知道为什么?

我认为你的参数值没有发送nil值,我也在使用afNetworking Get方法,请参考下面的代码,

viewDidLoad 给出以下代码:

NSURL *url = [NSURL URLWithString:@"http://localhost:5000/index"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //AFNetworking asynchronous url request
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        movies = [responseObject objectForKey:@"enquiry"];

        NSSortDescriptor *titleSort = [[NSSortDescriptor alloc] initWithKey:@"FirstName"
                                                                  ascending:YES selector:@selector(caseInsensitiveCompare:)];
//        NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects: titleSort, nil];
        movies=[NSMutableArray arrayWithArray:[movies sortedArrayUsingDescriptors:@[titleSort]]];

        NSLog(@"The Array: %@",movies);
        [self.mytableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Request Failed: %@, %@", error, error.userInfo);

    }];

    [operation start];

别忘了 #import "AFNetworking.h" 在 header 希望对你有帮助

终于发现问题真的是版本不对Xcode。在 Xcode 6 中它无法识别 nullable。升级我的 Xcode 刚刚解决了问题。