ios - Kingfisher nil 同时展开可选值
问题描述
我有一个Medicine
结构,其中包含image_origin
要下载并设置为 ImageView 的图像的 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
在上面的代码m
中是一个NSManagedObject
of CoreData
。我尝试从 CoreData 获取图像 URI 并将其设置为 ImageView,但每次在第 2 行我都会收到以下错误消息:Unexpectedly found nil while unwrapping an Optional value
我尝试更改变量和可选类型,尝试对 URI 进行硬编码但没有成功。
我究竟做错了什么?
PS我正在使用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)
}
推荐阅读
- c++ - 我的 c++ 编译器给出了正确的输出,但 c++ 的 codeforces 编译器给出了错误的输出
- python - Google Cloud Functions - Cloud SQL 中止连接
- android - 如果 react-native DeviceMotion 不可用怎么办?
- ios - 未找到模块“audio_session”
- r - 在R中测量给定日期周围的股票收益
- python-3.x - Python脚本可以在控制台运行,但双击.py文件后无法运行
- web - 使用 PWA 从操作系统打开文件
- asp.net - 转发器会导致整个页面的回发,即使它位于更新面板内
- spring-boot - 使用 Spring Boot 使用 Vault 进行 UserPass 身份验证
- vagrant - '在 Ubuntu 20.04 中的 PATH 变量中找不到'可执行的'bsdtar' Vagrant 试图运行