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 に答える