gpt4 book ai didi

ios - NSJSON序列化: unexpectedly found nil while unwrapping an Optional value

转载 作者:行者123 更新时间:2023-11-30 13:34:04 24 4
gpt4 key购买 nike

通常情况下,以下代码可以正常工作,但有时我认为,JSON 返回 nil 并遇到 fatal error 。如果数据为零我该如何处理。

我在以下行遇到 EXC_BAD_INSTRUCTION 错误:

let profileJsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray

感谢您的帮助。

do {
let profileJsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray

if let parseJSONArray = profileJsonArray {
self.myProfileinfo = parseJSONArray

let name: String? = self.myProfileinfo[0].valueForKey("fullname") as? String
let messsage: String? = self.myProfileinfo[0].valueForKey("message") as? String
let infourl: String! = self.myProfileinfo[0].valueForKey("thumb") as? String
let prourl: String! = self.myProfileinfo[0].valueForKey("prothumb") as? String
let email: String? = self.myProfileinfo[0].valueForKey("email") as? String ...

最佳答案

读取网络响应时,应先检查是否有错误。

NSSession 例如:

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if error {
// Deal with error, e.g.
print(error)
}
else {
do {
// Use data or response
let profileJsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray
// ....
}
catch {
// Deal with error
}
}
})
task.resume();

关于ios - NSJSON序列化: unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36279034/

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