【问题标题】:iOS - Incompatible block pointer types in AFNetworking GET methodiOS - AFNetworking GET 方法中不兼容的块指针类型
【发布时间】:2016-04-30 03:07:21
【问题描述】:

我正在学习使用 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;
    }];

但是,我得到了错误

不兼容的块指针类型将“void (^)(NSURLSessionTask *__strong, NSError *__strong)”发送到“void (^ __nullable)(NSURLSessionDataTask * __nonnull __strong)”类型的参数

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

【问题讨论】:

  • 查看故障块。参数应该是什么?你在用什么?看到不同?错误显示了差异。它们相似但不同。
  • 如果要获取html页面怎么办?参数应该是什么?
  • 刚才我检查了文档。 Xcode 6不支持AFNetworking 3.0+吗?

标签: ios objective-c iphone xcode afnetworking


【解决方案1】:

最后我发现问题确实是错误的版本Xcode。在Xcode 6 中,它无法识别nullable。升级我的Xcode 刚刚解决了这个问题。

【讨论】:

    【解决方案2】:

    我认为你的参数值不发送 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" 希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2015-11-28
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多