退出函数后NSMutable数组不保留数据

时间:2017-09-02 19:49:53

标签: ios objective-c

我有一个tableView,我正在尝试填充数据。我正在为函数中的数组赋值。一直保存到它离开System.out.println(lista03.size()); 功能。看一下日志,它让我觉得它与订购有关,比如我没有足够快地填充我的数据。或许这不是原因,我真的不知道。

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];
}

1 个答案:

答案 0 :(得分:1)

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

[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]);

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