我正在使用CloudKit,在加载数据时,我正在检查代码中的CKAsset是否为0,例如:
let img = result.value(forKey: "Picture") as! CKAsset
if img != nil {
}并得到以下错误:
“将非可选值类型'CKAsset‘与’nil‘进行比较总是返回true。
我知道这与选项有关,但无法找到解决办法。
发布于 2017-07-07 00:41:00
img不能是nil,因为您正在强制将其强制传递给CKAsset。当然,如果result.value(forKey: "Picture")返回nil或它实际上不是CKAsset,应用程序将在运行时崩溃。
编写代码的正确方法如下:
if let img = result.value(forKey: "Picture") as? CKAsset {
// do something with img
} else {
// there is no Picture value or it's not actually a CKAsset
}https://stackoverflow.com/questions/44960790
复制相似问题