条件绑定的初始化程序必须具有可选类型,而不是“() -> Data?”

Initializer for conditional binding must have Optional type, not '() -> Data?'

我在将图像上传到 Firebase storage/database 时遇到问题。我在创建这行代码时收到此问题:

if let uploadData = UIImage.pngData(self.profileImage.image!) {
}

此行的错误如标题所述:条件绑定的初始化程序必须具有可选类型,而不是“() -> 数据?”

关于如何解决这个问题有什么建议吗?

你可以试试

guard let uploadData = profileImage.image?.pngData() else { return }

如果你需要 if 让

if let uploadData = profileImage.image?.pngData() {

} 

pngData()UIImage实例

的实例方法

https://developer.apple.com/documentation/uikit/uiimage/1624096-pngdata

在您的代码中,您认为它是一种 class 方法,这是不正确的