问题描述
您好,我的应用程序发生致命错误:意外地发现nil,同时隐式展开了Optional值错误。我了解到选项存在问题。但是我不知道该如何更改,您能帮我吗?
func checkDownloadResponse(_ data:Data){
let dictionary: NSDictionary!=(try? JSONSerialization.jsonObject(with: data,options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary
let action_str=dictionary.value(forKey: "Action") as! Nsstring
**ERROR Thread 9: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value**
if(action_str == "1"){
let languageDictionary: NSDictionary = ["Action":(dictionary.value(forKey: "Action")! as AnyObject).description,"LanguageLabels":dictionary.value(forKey: "LanguageLabels")!]
let generalDataDictionary: NSDictionary = ["Action":(dictionary.value(forKey: "Action")! as AnyObject).description,"DataSource":dictionary.value(forKey: "ConfigData")!]
}else{
if(dict.value(forKey: "Error") != nil ){
let error_str=dictionary.value(forKey: "Error") as! Nsstring
//code
}else{
//code
}
}
}
这是出错的地方
ref = db.products(category: category.id)
解决方法
要解决id上的展开问题,那么:
if let value = ref {
value = db.products(category: category.id)
}