Swift Xcode迫使我打开一个非可选值
我正在使用Swift和Firebase创建一个基本的博客应用程序来练习我的编程技能 以下是我的用户模型:Swift Xcode迫使我打开一个非可选值,swift,firebase,optional,Swift,Firebase,Optional,我正在使用Swift和Firebase创建一个基本的博客应用程序来练习我的编程技能 以下是我的用户模型: class UserProfile { var uid: String var username: String var photoURL: URL init(uid: String, username: String, photoURL: URL){ self.uid = uid self.username = usernam
class UserProfile {
var uid: String
var username: String
var photoURL: URL
init(uid: String, username: String, photoURL: URL){
self.uid = uid
self.username = username
self.photoURL = photoURL
}
}
下面是xcode给我的错误代码块:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let line = line {
ImageService.downloadImageFrom(url: self.line?.author.photoURL, completion: { (image) in
self.imageView.image = image
}) // The error is in this line.
self.username.text = line.author.username
self.park.text = line.park
self.attraction.text = line.attraction
self.timestamp.text = line.createdAt.calendarTimeNow()
}
}
正如您所注意到的,“UserProfile”类的“photoURL”属性不是可选的。Xcode要求我使用(self.line?.author.photoURL)!强制展开此非可选值。可能会发生什么?我错过了什么
非常感谢您的关注。问题不在于
photoURL
。问题是,尽管正确地展开了可选属性行
,您还是错误地尝试使用仍然可选的self.line
。因为这是可选的,self.line?.author.photoURL
作为一个整体是可选的
只需将self.line?.author.photoURL
更改为line.author.photoURL