0

程序在下面的注释行崩溃。不知道出了什么问题。给第一次程序员的任何提示?

- (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;

} @结尾

4

1 回答 1

1

截至此消息:'-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance看起来用户不是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);
}
于 2013-02-18T00:25:26.147 回答