尝试从URL调用JSON数据时SIGABRT崩溃

时间:2012-08-18 16:02:20

标签: ios xcode json sigabrt

我正在开发一个将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                                                              选项:kNilOptions                                                                误差:&安培;错误];

    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 \ u90b \ u30ea \ u30d5 \ u30a9 \ u30fc \ u30e0“}],”option1“:”“}

以下是崩溃时返回的完整错误: 2012-08-19 00:32:59.421 CameraTest [2141:707] - [__ NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例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"];

1 个答案:

答案 0 :(得分:0)

我非常确定tagsNSArray还是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"];