2016-04-30 297 views
1

我正在学习使用AFNetworking并执行GET请求。iOS - 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; 
    }]; 

但是,我得到了错误

不兼容的块指针类型发送“无效(^)(NSURLSessionTask * __强, NSError * __ strong)'到类型'void(^ __nullable)(NSURLSessi onDataTask * __nonnull __strong)'

尝试了很多次,但无法弄清楚为什么?

+0

看看失败块。应该是什么参数?你在用什么?看到不同?错误显示了差异。它们相似但不同。 – rmaddy

+0

如果我想要获取html页面,该怎么办?参数应该是什么? –

+0

刚才我检查了文档。 'Xcode 6'不支持'AFNetworking 3.0 +'吗? –

回答

2

最后我发现,问题确实是错误的版本Xcode。在Xcode 6它不承认nullable。升级我的Xcode刚刚解决了这个问题。

0

我觉得你的参数值不发送零值,我也使用afNetworking获取方法,请参考下面的代码,

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"在标题 希望它的帮助