gpt4 book ai didi

ios - Kingfisher nil 在展开可选值时

转载 作者:行者123 更新时间:2023-11-28 10:39:23 39 4
gpt4 key购买 nike

我有 Medicine 结构,它包含我要下载并设置到 ImageView 的图像的 image_origin URL。出于下载/缓存目的,我使用了 Kingfisher 框架。

    let m = medicines[indexPath.row]

cell.medicineImage.kf.setImage(with: URL(string: m.value(forKeyPath: "image_origin") as! String)!)
cell.medicineName.text = m.value(forKeyPath: "i_name") as? String

在上面的代码中,mCoreDataNSManagedObject。我尝试从 CoreData 获取图像 URI 并将其设置为 ImageView,但每次在第 2 行我都会收到以下错误消息:Unexpectedly found nil while unwrapping an Optional value

我试过更改变量和 Optinal 类型,尝试硬编码 URI 但没有成功。

我做错了什么?

附言我正在使用 Swift4

最佳答案

如果您没有正确获取 urlString,只需安全地解包以修复崩溃并检查您的数据库,

if let urlString = m.value(forKeyPath: "image_origin") as? String {
print(urlString)
guard let url = URL(string: urlString) else { return }
cell.medicineImage.kf.setImage(with: url)
}

关于ios - Kingfisher nil 在展开可选值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51459738/

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