ObjectAtIndex崩溃

时间:2013-02-18 00:21:55

标签: ios objective-c

程序崩溃,下面有评论。不确定是什么问题。第一次编程的任何提示?

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

更多代码(不是全部代码)As Requested

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

1 个答案:

答案 0 :(得分: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);
}