【发布时间】:2019-06-10 16:45:28
【问题描述】:
我的图片中有空的 url 字符串。我将 url 字符串 图像保存在 firestore 中。但有时会发生没有图片,我需要检查并返回一个空图片。
当我加载视图时,我收到错误 nil。因为我在 firestore 中的变量是空的。
这是我的代码:
guard let dataLogoImage = try? Data(contentsOf: URL(string: self.hall!.studioHallLogo)!) else { return }
稍后我将dataLogoImage 应用于变量tempLogoImage。
self.tempLogoImage = UIImage(data: dataLogoImage)
如何检查Data 或空网址字符串 并避免nil?
【问题讨论】:
-
切勿使用
Data(contentsOf:)通过网络从远程 URL 获取数据。该初始化程序应该只用于文件系统的本地 URL。使用URLSession.dataTask从远程 URL 下载图像(或任何其他数据)。