gpt4 book ai didi

ios - 实例变量总是返回零

转载 作者:行者123 更新时间:2023-11-28 20:17:39 25 4
gpt4 key购买 nike

我有一个实例变量数组:

       @interface ChannelsVC ()
{
NSArray *arrayofFrequencies;
}
}
@end

@implementation ChannelsVC
......

在“viewDidLoad”中进行分配

arrayofFrequencies=[[NSArray alloc]init];

然后,我从 JSON 数据源向数组中插入数据:

-(void)GetFrequencies{

NSString *urla=@"http://xxxxxxxx";
NSString *uria = [urla stringByAppendingString:self.lien_satellite];
NSURL *urlFrequencies= [ NSURL URLWithString:uria];

NSLog(@"Frequencies URI: %@",uria); //correct URI



NSURLRequest *request = [NSURLRequest requestWithURL:urlFrequencies];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
arrayofFrequencies=[JSON valueForKeyPath:@"frequency"];

NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]);//the count here is correct, I have data in the array

} failure:nil];

[operation start];

}

问题是,当想要创建一个 UITableView,其部分数为“[arrayofFrequencies count];”时,它总是返回“0”!它是一个实例变量,它在“GetFrequencies”函数之外返回零。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"number of sections %ld",(long)[arrayofFrequencies count]); // always returns 0
return [arrayofFrequencies count];
}

我做错了什么吗?

感谢您的帮助。

控制台输出:

013-06-25 12:58:00.163 Frequencies[840:c07] number of sections 0
2013-06-25 12:58:00.165 Frequencies[840:c07] number of sections 0
2013-06-25 12:58:00.641 Frequencies[840:c07] Frequencies count: 4

最佳答案

之后调用[tableView reloadData]

NSLog(@"Frequencies count: %lu", (unsigned long)[arrayofFrequencies count]);//the count here is correct, I have data in the array

要理解的一点是在 viewDidLoad 中调用 tableView 委托(delegate)。那时数组计数为零。后来数组充满了对象。 reloadData 方法再次调用表的委托(delegate)方法。

关于ios - 实例变量总是返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17296630/

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