应用程序因异常NSInvalidArgumentException而崩溃

时间:2012-11-05 22:38:59

标签: iphone ios xcode json exception

我从Web服务获取数据但是在URL请求时崩溃了,请参阅下面的代码

- (void)loadDataSource  
 {

   // Request  
    NSString *URLPath = [NSString stringWithFormat:@"http://imgur.com/gallery.json"];  
    NSURL *URL = [NSURL URLWithString:URLPath];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];

**CRASHED HERE**    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)  
 {

        NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];

        if (!error && responseCode == 200) {
            id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            if (res && [res isKindOfClass:[NSDictionary class]]) {
                self.items = [res objectForKey:@"gallery"];
                [self dataSourceDidLoad];
            } else {
                [self dataSourceDidError];
            }
        } else {
            [self dataSourceDidError];
        }
    }];
}    

请帮帮我

提前致谢

1 个答案:

答案 0 :(得分:0)

您的代码是正确的,但结果中没有关键字@“gallery”的对象。看起来关键是@“数据”。所以将代码更改为

self.items = [res objectForKey:@"data"]