尝试从数据库显示图片时发生致命错误

问题描述 投票:-1回答:1

因此,我试图显示用户选择并上传到Firebase数据库的图像。我有这段代码片段,显示了他们的用户名,标签和个人资料图像。这是出现错误的屏幕截图。我查看了有关此错误的其他表格,但尝试应用此错误,仍然遇到相同的问题。感谢您提出有关如何提前解决此问题的建议!

<< [setProfilePic Func
swift xcode fatal-error
1个回答
0
投票
[看起来data最有可能是nil,因此UIImage()没有被创建。您可以测试它并抛出错误,如果合适的话:

enum DataError: Error { case noData } guard let data = NSData(contentsOf: NSURL(string: databaseProfilePic)!) else { print("Could not create valid NSData from databaseProfilePic") throw DataError.noData }

如果您不喜欢guard,则可以考虑分配if

if let data = NSData(contentsOf: NSURL(string: databaseProfilePic)!) { self.setProfilePic(imageView:self.imageView1, imageToSet:UIImage(data: data as Data)!) }

我正在查看URL databaseProfilePic和获取该URL的结果。

0
投票
[尝试像databaseprofilePic一样解开if let的值

if let profilePic = dict!["photoURL"] as? String { //your code to set profile pic }

© www.soinside.com 2019 - 2024. All rights reserved.