我正在学习使用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)'
多次尝试但无法找出原因?
答案 0 :(得分:2)
最后我发现问题确实是错误的版本Xcode
。在Xcode 6
中,它无法识别nullable
。升级我的Xcode
只是解决了问题。
答案 1 :(得分:0)
我认为你的参数值不发送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"
希望它有用