gpt4 book ai didi

ios - NSMutable 数组在退出函数后不保留数据

转载 作者:行者123 更新时间:2023-11-29 00:12:35 27 4
gpt4 key购买 nike

我有一个 tableView,我正在尝试用数据填充它。我正在为函数内的数组赋值。所有内容都会保存起来,直到它离开 searchWithLocation 函数。查看日志让我认为这与排序有关,就像我没有足够快地填充数据一样。或者也许这不是原因,我真的不知道。

- (void)viewDidLoad {
[super viewDidLoad];

venueName = [[NSMutableArray alloc] init];

[YLPClient authorizeWithAppId:@"sdgsfgfgfg"
secret:@"aegsgdgdfs"
completionHandler:^
(YLPClient *client, NSError *error) {
// Save your newly authorized client
self.client = client;

[self.client searchWithLocation:@"Chicago"
completionHandler:^
(YLPSearch *search, NSError *error) {
for (YLPBusiness *business in search.businesses) {
[venueName addObject:business.name];
}
NSLog(@"I return results: %lu", (unsigned long)[venueName count]);
}];
NSLog(@"I return 0: %lu", (unsigned long)[venueName count]);
}];
}

...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"number of rows in section: %lu", (unsigned long)[venueName count]);
return [venueName count];
}

日志:

2017-09-02 14:46:39.708 whatever[67890:8535793] number of rows in section: 0
2017-09-02 14:46:39.714 whatever[67890:8535793] number of rows in section: 0
2017-09-02 14:46:39.715 whatever[67890:8535793] number of rows in section: 0
2017-09-02 14:46:40.448 whatever[67890:8535846] I return 0: 0
2017-09-02 14:46:41.174 whatever[67890:8535846] I return results: 20

最佳答案

你对异步程序执行有误解。每个完成处理程序都可以在“调用”执行路径已经终止后执行。对于您的情况:

[self.client searchWithLocation:@"Chicago"
completionHandler:
// This completion handler can be executed after seconds, minutes or even hours.
^(YLPSearch *search, NSError *error)
{
NSLog( @"As times go by – sometime in the future" );
for (YLPBusiness *business in search.businesses)
{
[venueName addObject:business.name];
}
NSLog(@"I return results: %lu", (unsigned long)[venueName count]);
}];
// This path is executed immediately. esp. likely before the cmpletion handler is executed
NSLog( @"It is now or never" );
NSLog(@"I return 0: %lu", (unsigned long)[venueName count]);

将完成处理程序后面的代码移到其中。

关于ios - NSMutable 数组在退出函数后不保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46017607/

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