ios - 从存储中下载图像时出现可选错误,我已将字符串转换为 URL

标签 ios swift firebase google-cloud-firestore storage

从 firebase 存储下载图像时可选错误,我正在将字符串转换为 URL 以下载图像

这是发生错误的代码,如果需要更多代码请告诉我

let imageUrl = URL(string: post._postuserprofileImagUrl)
        ImageService.getImage(withURL: imageUrl) { image in
            self.profileImageView.image = image
        }

最佳答案

您必须(安全地)解包 URL 实例

if let imageUrl = URL(string: post._postuserprofileImagUrl) {
        ImageService.getImage(withURL: imageUrl) { image in
            self.profileImageView.image = image
        }
}

甚至(如果 postuserprofileImagUrl 也是可选的)

if let userprofileImagUrl =  post._postuserprofileImagUrl,
   let imageUrl = URL(string: userprofileImagUrl) {
        ImageService.getImage(withURL: imageUrl) { image in
            self.profileImageView.image = image
        }
}

关于ios - 从存储中下载图像时出现可选错误,我已将字符串转换为 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57873344/

相关文章:

ios - 尾随约束缺失错误

ios - 线路交叉点 - GPS 坐标

android - Firebase Analytics 甚至会出现在日志中,但不会出现在 Firebase 控制台上

ios - 想要延迟函数调用

iPhone 影音播放器

ios - 在 Today Extension (iOS) 中设置 NSUserDefaults

ios - 即使数据模型包含实体,mogenerator 也不会生成任何实体。报告 "No entities found in model. No files will be generated."

swift - 来自泛型类型的初始化器不会在 swift 中被继承?

ember.js - 如何组织我的模型

android - Android 6 和 5 上的 Firebase InApp 消息传递崩溃