gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value

转载 作者:行者123 更新时间:2023-12-01 21:17:38 24 4
gpt4 key购买 nike

运行代码时遇到此错误: fatal error :在解包可选值时意外发现 nil,可能是什么原因导致的?

var bodyData = "tel_client=\(phoneNumber)&id_pays=\(phoneCode)&datetime_alibi=\(getDateTimeFormat(date))&tel_contact=\(to)&nb_rappel_alibi=1&pr_rappel_alibi=5"
//println(bodyData)
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
(response, data, error) in
var error: NSError?
var jsonData: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &error) as NSDictionary

if jsonData["success"] as Int == 1 {
let alertView = UIAlertView(title: "GOOD", message: nil, delegate: self, cancelButtonTitle: "ok")
alertView.show()
} else {
let alertView = UIAlertView(title: "ERROR", message: jsonData["message"] as? String, delegate: self, cancelButtonTitle: "ok")
alertView.show()
}
}

最佳答案

JSONObjectWithData 返回一个可选值。这就是你的错误的来源。打开它就会消除错误。

var jsonData = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &error) as? NSDictionary

// unwrap the optional
if let json = jsonData {
if json["success"] as Int == 1 {
let alertView = UIAlertView(title: "GOOD", message: nil, delegate: self, cancelButtonTitle: "ok")
alertView.show()
} else {
let alertView = UIAlertView(title: "ERROR", message: json["message"] as? String, delegate: self, cancelButtonTitle: "ok")
alertView.show()
}
} else {
// jsonData was nil
}

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

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