【发布时间】:2021-04-13 18:04:55
【问题描述】:
在某些 iPhone 设备中并非全部发生崩溃,并且崩溃指出这一行并且消息显示“在展开可选值时意外发现 nil”。下面是快速代码
let location = userDefaults.object(forKey: "itemRes") as! [String] //this line is getting crash
requestModel.second_res_lat = location[0].description
requestModel.second_res_lon = location[1].description
【问题讨论】:
-
userDefaults.object(forKey: "itemRes")无法转换为字符串数组 -
@aheze 有些设备没有崩溃,有些设备崩溃了,那么解决这个问题的解决方案是什么?
-
不要强制解包,而是使用保护:
let location = userDefaults.object(forKey: "itemRes") as? [String] else { /* No such setting, or it cannot be converted to array of strings */ return }。 -
@MohammedNabil 试试
print("itemRes is \(userDefaults.object(forKey: "itemRes"))")看看它是什么。这可能不是你所期望的
标签: ios swift userdefaults