【问题标题】:app crashed due to exception NSInvalidArgumentException应用程序因异常 NSInvalidArgumentException 而崩溃
【发布时间】:2012-10-25 20:18:02
【问题描述】:

我正在从 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];
        }
    }];
}    

请帮帮我

提前致谢

【问题讨论】:

    标签: iphone ios xcode json exception


    【解决方案1】:

    您的代码是正确的,但是结果中没有键 @"gallery" 的对象。看起来关键是@“数据”。所以把你的代码改成

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

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2018-12-11
      • 2015-09-22
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多