Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Objective-C AFNetworking dataTaskWithRequest:completionHandler:can';t从完井区块内部检索数据_Ios_Objective C_Json_Afnetworking - Fatal编程技术网

iOS Objective-C AFNetworking dataTaskWithRequest:completionHandler:can';t从完井区块内部检索数据

iOS Objective-C AFNetworking dataTaskWithRequest:completionHandler:can';t从完井区块内部检索数据,ios,objective-c,json,afnetworking,Ios,Objective C,Json,Afnetworking,这是我的密码: //create array with all the teams NSMutableArray *leagueTeams = [[NSMutableArray alloc] init]; //send request [[sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

这是我的密码:

 //create array with all the teams
NSMutableArray *leagueTeams = [[NSMutableArray alloc] init];
//send request

[[sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    NSDictionary *responseFromJSONDictionary = (NSDictionary *) responseObject;

    //copy the team's attributes into temp variables
    NSString *tempTeamIDCode = [responseFromJSONDictionary objectForKey:@"teamCode"];
    NSString *tempTeamName = [responseFromJSONDictionary objectForKey:@"teamName"];
    NSInteger tempTeamPoints = [(NSNumber *) [responseFromJSONDictionary objectForKey:@"teamPoints"] integerValue];

    //use temp variables to create a temporary team
    Team *aTeam = [[Team alloc] initWithTeamIdCode:tempTeamIDCode andTeamName:tempTeamName andLeaguePoints:tempTeamPoints];

    //add team to array
    [leagueTeams addObject:[aTeam copy]];

}]resume];
我正在尝试制作一个从服务器检索JSON数据的应用程序。现在我使用静态JSON来检索条目。我使用断点跟踪变量值。应用程序成功地检索JSON数据,它成功地创建了3个临时变量,成功地创建了团队对象,并成功地将该对象添加到leageTeams mutablearray中,同时在成功代码块中

但是,当应用程序离开成功块时,leagueTeams数组就会消失。它不存在于内存中,即使它是一个空数组,就像它在执行成功块之前所做的那样


我可能在试图将数据传递给代码块内的外部变量时犯了一些错误,但所有其他类似问题都遇到了无法及时从服务器获取数据的问题,但在我的例子中,数据请求总是成功的,JSON响应和将其转换为NSDICtionary都可以很好地工作……所以有人可以帮忙吗?谢谢

好的,现在发生的是“dataTaskWithRequest:completionHandler:”方法是异步的!一旦它开始执行程序,就不会等待它返回值,而是继续执行它之外的下一行。因此,您在该方法下面编写的代码可能比完成处理程序中的代码先执行。因此,您可以在完成处理程序返回所需的值后触发通知或调用某个委托方法来运行任何代码。

将aTeam回调添加到主线程时,将
\u块添加到数组的定义中,以及在完成块中<代码>[NSoperationQueue mainQueue]添加操作块:
使用通知中心完成并重新加载表。谢谢