exc_bad_instruction

时间:2015-10-11 19:19:10

标签: json multithreading swift

func setlLabels(weatherData: NSData) {
    var jsonError: NSError?   
    let json = NSJSONSerialization.JSONObjectWithData(weatherData, options: nil, error: &jsonError) as! NSDictionary
    // ...
}

在行中构建“让json ...”后出现错误,如“thread 1 exc_bad_instruction(code = exc_i386_invop subcode = 0x0)”

我认为这是与NSDicitionary相关的问题,但我不知道如何解决这个问题。

Screenshot with the error

1 个答案:

答案 0 :(得分:0)

当您的应用中发生意外情况且应用崩溃时,会导致此错误。为了解决这个问题,有一些变量不应该是零,你强行打开它。 (!)这是固定版本的样子:

func setlLabels(weatherData: NSData) {
    var jsonError: NSError?   
    let json = NSJSONSerialization.JSONObjectWithData(weatherData, options: nil, error: &jsonError) as? NSDictionary
    // ...
}

如您所见,当您在!之后移除as并将其替换为?时,它不会崩溃。

相关问题