gpt4 book ai didi

ios - 无法从解析代码块中获取数组 PFQuery 对象

转载 作者:行者123 更新时间:2023-11-29 02:44:06 25 4
gpt4 key购买 nike

我不知道解析是怎么回事,但出于某种原因,它不允许我将检索到的数组保存到我创建的可变数组中。它在解析代码块内部工作,但一旦在外部,它就会显示 null。请帮助?

 PFQuery *query = [PFQuery queryWithClassName:@"comments"];
[query whereKey:@"flirtID" equalTo:recipe.flirtID];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {


comments = [[NSMutableArray alloc]initWithArray:objects];

// Do something with the found objects
for (PFObject *object in objects) {

}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
NSLog(@"%@",[comments objectAtIndex:0]);

最佳答案

它实际上正在按预期工作。您应该阅读有关 block 如何工作的信息。

编辑:尝试阅读 Apple's Documentation

你在 NSLogging 'comments' before comments 实际设置。这是如何运作的?你看,查询是在后台运行的,它实际上需要一点时间。它异步运行。但是 block 外的代码会立即运行。

虽然代码在前面,但因为它是一个异步 block ,所以它可以而且将在任何时候运行。

试试这个:

comments = [[NSMutableArray alloc]initWithArray:objects];
NSLog(@"%@",[comments objectAtIndex:0]);

重要的问题是,查询之后你想做什么?看起来你想保存评论,但是然后呢?这将决定你下一步做什么。

关于ios - 无法从解析代码块中获取数组 PFQuery 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com