我使用AFNetworking与我的网络服务进行通信,使用以下代码:
-(NSMutableDictionary*)getData:(NSString*)userID{
__block NSMutableDictionary *data;
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:@"http://webservice.com/api/"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient getPath:@"data" parameters:@{@"type":@"1", @"id":userID}
success:^(AFHTTPRequestOperation *operation, id JSON) {
data = (NSMutableDictionary *) JSON;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error occured!");
}];
return data;
}
调用此方法会出现以下错误: [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象插入nil对象[0]'
这是什么意思?这不是一个失败,所以我不确定它的Web服务调用是否出错。
答案 0 :(得分:3)
确保userID不是nil。
首先记录请求有效负载和服务器响应通常是一种很好的做法,以确保一切正常运行。
答案 1 :(得分:-1)
我的猜测是您将JSON
变量投射到NSDictionary
。这是我所指的那条线:
data = (NSMutableDictionary *) JSON;
相反,您可能想尝试:
[NSJSONSerialization JSONObjectWithData:JSON
options:kNilOptions
error:&error];
然后将其保存到data
变量。