【问题标题】:"Thread 1: signal SIGABRT" error with JSON serialization“线程 1:信号 SIGABRT”错误与 JSON 序列化
【发布时间】:2016-07-29 14:57:16
【问题描述】:

我正在尝试使用 Wea​​ther Underground API 将天气数据解析到我的应用程序中,使用 Xcode 7.3.1、iOS 9.3 和 JSON(但我在使用其他 API,例如 OpenWeatherMap 时遇到了同样的问题)。

我在构建应用程序时没有收到错误,但是当我在模拟器中调用天气时,我收到“线程 1:信号 SIGABRT”错误。我使用断点推测我的问题来自序列化。

我已经尝试清理我的项目,但我没有双重连接。

当我下载并运行this教程的项目时,我遇到了同样的问题......

这是我的代码:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *affichermeteo;
@property (weak, nonatomic) IBOutlet UILabel *meteo;

@end



@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];


}

- (IBAction)affichermeteo:(id)sender {

NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
                              [NSURL     URLWithString:@"http://api.wunderground.com/api/e5cdee14984e242b/conditions/q/CA/San_Francisco.json"]];

NSError *error;

NSDictionary *allCourses = [NSJSONSerialization
                            JSONObjectWithData:allCoursesData
                            options:NSJSONReadingMutableContainers
                            error:&error];



if( error )
{
    NSLog(@"%@", [error localizedDescription]);
}
else {
    NSArray *currentobservation = allCourses[@"estimated"];
    for ( NSDictionary *theCourse in currentobservation )
    {
        _meteo.text=theCourse[@"weather"];

    }
}



}



@end

我的错误窗口:

Here

提前感谢您的帮助,对不起我的英语,我是法国人!

【问题讨论】:

  • 哪条线路崩溃了?您可以编辑您的问题以添加崩溃中的堆栈跟踪吗? (您可能需要在异常上设置断点以判断是哪一行导致崩溃。)
  • 崩溃出现在“options:NSJSONReadingMutableContainers”行。
  • 这表明服务器正在为您提供格式错误的 JSON。您可以将内容输入不同的 JSON 解析器或 JSON 验证器吗?
  • 顺便说一句,您用于从服务器读取 JSON 的代码不适用于生产应用程序。 NSData initWithContentsOfURL 是同步调用,会导致 UI 冻结,直到数据完全加载。如果出现网络故障,冻结时间可能长达 2 分钟,系统将终止您的应用程序。您需要更改该代码以使用 NSURLSession。
  • 是的,在线 JSON 解析器显示 JSON 内容没有任何问题

标签: ios json xcode serialization sigabrt


【解决方案1】:

我可以使用您的代码获取数据。您缺少 iOS 9 新添加的 App Transport Security 标志。

添加 App Transport Security Settings 键并将 Allow Arbitrary Loads 标记为 是的就像下面的截图:

这应该可以解决您的问题。

查看this 链接以了解有关应用程序传输安全性的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多