控制台继续输出在 Swift 中包装为可选的字符串,为什么?



请帮忙,我真的很挣扎,已经阅读了多个线程和图特,但似乎找不到问题。我应用了与应用程序上的另一个代码块相同的方法。

当我从源ViewController实例化ViewController时,我正在用值设置vardictionary

但是控制台继续输出包装为可选的字符串,因此也在 UI 上输出,为什么?

sender.name: Liam, 01/13/1990, Optional("Actor")

使用值设置dictionary

var dictionary: [String : Any]!{
didSet{
print("inside InviteVC")

inviteType = dictionary["type"] as? String
inviteId = dictionary["complimentId"] as? String
status = dictionary["status"] as? Bool
timeStamp = dictionary["timeStamp"] as? Int
sender = dictionary["fromUser"] as? User
print("sender.name: (sender!.firstName), (sender!.birthday), (String(describing: sender?.occupation))")
}//end didSet
}//end var

这是我的用户模型:

struct User {
let uid: String
let name: String
let email: String
let profilePictureURL: String
var occupation: String?
let birthday: String
let firstName: String
let lastName: String
let gender: String
let discoverable: Bool
let online: Bool
let discoveryPrefs: [String : Any]
var profileImages = [String]()
init(uid: String, dictionary: [String: Any]) {
self.uid = uid
self.name = dictionary["name"] as? String ?? ""
self.email = dictionary["email"] as? String ?? ""
self.profilePictureURL = dictionary["profilePictureURL"] as? String ?? ""
self.occupation = dictionary["occupation"] as? String ?? ""
self.birthday = dictionary["birthday"] as? String ?? ""
self.firstName = dictionary["firstName"] as? String ?? ""
self.lastName = dictionary["lastName"] as? String ?? ""
self.gender = dictionary["gender"] as? String ?? ""
self.discoverable = dictionary["discoverable"] as? Bool ?? false
self.online = dictionary["online"] as? Bool ?? false
self.discoveryPrefs = dictionary["discoveryPrefs"] as? [String : Any] ?? [String : Any]()
self.profileImages = dictionary["profileImages"] as! [String]

}//end init
}//end class

这就是我构建用户对象的地方:

func getUserInfo(forUserId forId: String, handler: @escaping (User) -> ()) {
REF_USERS.child(forId).observeSingleEvent(of: .value, with: { (snapshot) in
//handle snapshot code here...
var occupa: String?
if let occupation = snapshot.childSnapshot(forPath: "occupation").value as? String {
occupa = occupation
} else {
occupa = ""
}

let dictionary: [String : Any] = ["uid": uid, "name": name, "email": email, "profilePictureURL": profilePictureURL, "birthday": birthday, "firstName": firstName, "lastName": lastName, "gender": gender, "discoverable": discoverable, "online": online, "discoveryPrefs": discoveryPrefs, "profileImages": profileImages!, "occupation": occupa!]
let user = User(uid: uid, dictionary: dictionary)
handler(user)
}, withCancel: { (error) in
print(error)
})

}//end func

如果您查看打印值的行,您会注意到您正在尝试打印可选值。

print("sender.name: (sender!.firstName), (sender!.birthday), (String(describing: sender?.occupation))")

在最后一部分中,您尚未解开sender,这仍然是可选的,属性变量occupation也是可选的。将最后一部分替换为

print(sender?.occupation ?? "")

if let occupation = sender?.occupation {
print(occupation)
}

因为你的var occupation: String?是可选的。String(describing:object)"(object)"相同,如果对象是Optional键入,则文本将被Optional()换行。你必须打开它才能摆脱它。

最新更新