0

我正在开发一个将 JSON 数据带入 TableView 的应用程序。但是在 JSON 操作的代码开始的那一刻,应用程序崩溃并出现 SIGABRT 错误。下面是我正在使用的代码(当然,出于安全原因,减去了实际的 URL):

// START - CONNECT TO JSON FILE ON SERVER AND DOWNLOAD INFORMATION
- (void)fetchTags
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"URL goes here..."]];

        NSError* error;
        //tags = how many objects there are in the JSON file containing data
        tags = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];

NSDictionary *tagz = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    tags = [tagz objectForKey:@"name"];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}
// END - CONNECT TO JSON FILE ON SERVER AND DOWNLOAD INFORMATION

//START - NUMBER OF ROWS TO GENERATE
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tags.count;
}
//END - NUMBER OF ROWS TO GENERATE


// START - PULL IN INFORMATION FROM JSON AND DISPLAY IN CELLS
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TagCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *tag = [tags objectAtIndex:indexPath.row];
    NSString *name = [tag objectForKey:@"name"];

        if ([tag objectForKey:@"name"] != NULL)

    cell.textLabel.text = name;

    return cell;
    }
// END - PULL IN INFORMATION FROM JSON AND DISPLAY IN CELLS

这是 URL 指向的 JSON 数据: {"sessionid": "0.97880095305420120818085118", "option2": "", "logonresult": "ACCEPT", "projects": [{"projid": 2012011101, "name": "\u5909\u66f4\u5c65\u6b74\u30c6\u30b9\u30c8"}, {"projid": 2011101401, "name": "20111014-01"}, {"projid": 201111001, "name": "\u5c3e \u7530\u90b8\u30ea\u30d5\u30a9\u30fc\u30e0" }], "option1": ""}

这是崩溃时返回的完整错误:2012-08-19 00:32:59.421 CameraTest[2141:707] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x16f750 2012-08-19 00:32:59.423 CameraTest[ 2141:707] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x16f750”

谁能发现我做错了什么或向我推荐可能有帮助的教程?

提前致谢!

编辑:放置在 (void)fetchTags 中的以下代码修复了崩溃,但我仍然遇到问题,因为数据没有被拉入我的单元格:

    NSDictionary *tagz = [NSJSONSerialization JSONObjectWithData:data
                                                         options:kNilOptions
                                                           error:&error];

    tags = [tagz objectForKey:@"name"];
4

1 回答 1

0

我很确定那tags是一个NSArray或一个NSMutableArray,它应该是一个NSDictionary

您的应用程序崩溃的原因是由于[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];返回 NSDictionary 对象并且NSDictionary没有该objectAtIndex:方法,因此会触发异常。

你可以这样做:

NSDictionary *tagz = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];

tags = [tagz objectForKey:@"name"];
于 2012-08-18T18:14:31.873 回答