ios - AFNetworking 响应显示在日志中,但在调试器中为零
<p><p>我正在使用 AFNetworking 来获取 JSON 响应。我得到的是一个 PhotoPXArray(我使用地幔创建的模型)。日志输出正是我想要的数据。我的问题是使用数据。当我设置一个断点并查看 responseObject 时,它是 nil。我不知道为什么日志会抽出数据,但调试器中的值是 nil。</p>
<p>我最终要做的是保存响应以供以后使用。</p>
<p>另外,我正在使用 Sculptor 来帮助进行序列化。</p>
<pre><code>-(NSArray*) getPhotoForWord:(NSString*)word {
NSArray *results = nil;
__block NSMutableDictionary *test = nil;
NSString *requestString = BASE_URL;
requestString = ;
requestString = ;
requestString = ;
NSString *encoded = ;
AFHTTPRequestOperationManager *manager = ;
manager.responseSerializer = ;
[manager GET:encoded
parameters:nil
//success:^(AFHTTPRequestOperation *operation, id responseObject) {
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
test = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
return results;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你从不设置 <code>results</code> 变量,所以当然会返回 <code>nil</code>。即使您使用了 <code>test</code> (您确实设置了),这也是异步发生的,所以当您立即返回时,它也将是 <code>nil</code>,只获得 <code>responseObject </code> 稍后值。</p>
<p>您可以考虑采用异步模式,而是提供一个完成处理程序参数:</p>
<pre><code>- (void)getPhotoForWord:(NSString*)word completionHandler:(void (^)(id responseObject, NSError *error))completionHandler{
NSString *requestString = BASE_URL;
requestString = ;
requestString = ;
requestString = ;
NSString *encoded = ;
AFHTTPRequestOperationManager *manager = ;
manager.responseSerializer = ;
[manager GET:encoded parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completionHandler) {
completionHandler(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (completionHandler) {
completionHandler(nil, error);
}
}];
}
</code></pre>
<p>然后你会这样调用它:</p>
<pre><code>[obj getPhotoForWord:word completionHandler:^(id responseObject, NSError *error) {
// use responseObject here
}];
// do not use it here because the above happens asynchronously (i.e. later)
</code></pre></p>
<p style="font-size: 20px;">关于ios - AFNetworking 响应显示在日志中,但在调试器中为零,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/29050446/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/29050446/
</a>
</p>
页:
[1]