gpt4 book ai didi

ios - 对象索引崩溃

转载 作者:行者123 更新时间:2023-11-29 04:07:48 24 4
gpt4 key购买 nike

程序在下面的注释行处崩溃。不知道出了什么问题。对于第一次程序员有什么建议吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}

cell.textLabel.text = [[users objectAtIndex:indexPath.row] objectForKey:[[NSString alloc] initWithFormat:@"%d", indexPath.row]];
//My program runs but then crashes at this line, I have no idea what's wrong with it though "reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance"


return cell;
}

按要求提供更多代码(但不是全部)

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

users = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[self.tableView reloadData];
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [users count];
}

在.h文件中

@interface GIFUsersListViewController : UITableViewController {

NSArray *users;
NSMutableData *data;

} @end

最佳答案

截至此消息:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例看起来用户不是NSArray,而是NSDictionary.


如果您不确定 JSON 中的内容,请在分配给用户之前检查它:

id usersFromJSON = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
if ([usersFromJSON isKindOfClass:[NSArray class]]) {
users = usersFromJSON;
} else {
NSLog(@"Something wrong with my JSON: %@", usersFromJSON);
}

关于ios - 对象索引崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927753/

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