gpt4 book ai didi

ios - 没有互联网崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 06:21:39 26 4
gpt4 key购买 nike

我遇到了错误

fatal error: unexpectedly found nil while unwrapping an Optional value

未连接互联网时在这条线上。

let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary            // Line 


self.dict = jsonData;

self.array1 = (self.dict.objectForKey("results") as? NSMutableArray)!

dispatch_async(dispatch_get_main_queue()) {
self.table.reloadData()
}


} catch {

print(error)
}
})
task1.resume()

请帮助任何帮助将不胜感激

最佳答案

发生这种情况是因为您强制打开数据,这总是一个坏主意,因为您不知道它是否为nil

为了解决这个问题,您需要在尝试序列化 JSON 之前检查数据是否为 ​​nil:

// Checking if data is nil, and unwraping it
if let unwrappedData = data {
let jsonData = try NSJSONSerialization.JSONObjectWithData(unwrappedData, options: .MutableContainers) as! NSDictionary
// handle json here
}

或另一种方式:

if data == nil {
return
}
// else, data is not nil
let jsonData = try NSJSONSerialization...

关于ios - 没有互联网崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32942390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com