我正在使用AFNetworking AFHTTPSessionManager发出GET请求。当我记录响应对象时,将记录responseObject。当我尝试记录我的locationIDsDictionary时,我得到null。范围问题? locationIDsDictionary是否需要是_block类型?
viewController.h
@property (strong, nonatomic) NSDictionary *locationIDsDictionary;
viewController.m
[[LPAuthClient sharedClient]GET:getLocationIDString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
_locationArray = responseObject[@"data"];
NSLog(@"Response %@", responseObject);
});
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Failure %@", error);
}];
NSLog(@"locationIDsDictionary %@", _locationIDsDictionary);
编辑:输出
Response {
data = (
{
id = 45388148;
latitude = "37.785834";
longitude = "-122.406417";
name = "TESTING HATE VENUE";
},
{
id = 208947000;
latitude = "37.785834";
longitude = "-122.406417";
name = "The Greatest Isreal";
},
{
id = 86128772;
latitude = "37.785834";
longitude = "-122.406417";
name = "Pike street";
},
{
id = 33208867;
latitude = "37.785834";
longitude = "-122.406417";
name = abcde;
},
{
id = 104267842;
latitude = "37.785834";
longitude = "-122.406417";
name = "somewhere over south dakota";
},
{
id = 232446516;
latitude = "37.785834";
longitude = "-122.406417";
name = wow;
},
{
id = 107511313;
latitude = "37.785834";
longitude = "-122.406417";
name = "Yum Yum.com";
},
{
id = 189736241;
latitude = "37.785834";
longitude = "-122.406417";
name = "wow;l";
},
{
id = 246109339;
latitude = "37.785834";
longitude = "-122.406417";
name = Fused;
},
{
id = 153279132;
latitude = "37.785834";
longitude = "-122.406417";
name = 333;
},
{
id = 115790356;
latitude = "37.785834";
longitude = "-122.406417";
name = "Bar Karma";
},
{
id = 50341151;
latitude = "37.785834";
longitude = "-122.406417";
name = popoopo;
},
{
id = 121590347;
latitude = "37.785834";
longitude = "-122.406417";
name = 26;
},
{
id = 75286586;
latitude = "37.785834";
longitude = "-122.406417";
name = wow;
},
{
id = 72889805;
latitude = "37.785834";
longitude = "-122.406417";
name = test;
},
{
id = 284599098;
latitude = "37.785834009";
longitude = "-122.406416997";
name = "Urban Safari";
},
{
id = 89500038;
latitude = "37.785835266";
longitude = "-122.406417847";
name = "Cali Home";
},
{
id = 8736810;
latitude = "37.785835266";
longitude = "-122.406417847";
name = "Test%20Test%20Test";
},
{
id = 45031994;
latitude = "37.78584";
longitude = "-122.4064";
name = japaneses;
},
{
id = 35874897;
latitude = "37.785806574";
longitude = "-122.406477728";
name = "Hotel Palomar Fifth Floor Restaurant";
}
);
meta = {
code = 200;
};
}
答案 0 :(得分:0)
你写......
当我尝试记录locationIDsDictionary时,我得到null。
由于你没有共享这个日志,我假设你正在做这样的事情......
[self runMyGetLocationMethodAsynchronously];
NSLog(@"%@", _locationIDsDictionary);
这不起作用,因为您在异步方法的成功块中设置了_locationIDsDictionary
。在该块运行之前,不会设置您的字典。
答案 1 :(得分:0)
以CrimsonChris'为基础回答,GET: parameters: success:
方法是异步的。在这段代码中:
[[LPAuthClient sharedClient] GET:getLocationIDString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
_locationArray = responseObject[@"data"];
NSLog(@"Response %@", responseObject);
});
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Failure %@", error);
}];
NSLog(@"locationIDsDictionary %@", _locationIDsDictionary);
success
参数是您的完成块,所以:
^(NSURLSessionDataTask *task, id responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
_locationArray = responseObject[@"data"];
NSLog(@"Response %@", responseObject);
});
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Failure %@", error);
}
仅当iOS 完成下载任务时才会调用,因此只有在下载任务完成时才会分配字典。同时,剩余的代码行正在执行,因此异步的含义 - 在不同的线程上执行。如果它是同步的,那么代码将被停止,直到代码块完成为止,这很糟糕,你会发现它在iOS应用程序中被翻译成了freezy UI。
因此,在执行下载任务时仍然会调用NSLog(@"locationIDsDictionary %@", _locationIDsDictionary);
,因此评估为nil
- 当然除非您的网速疯狂 :)