请帮助,我真的很为此苦苦挣扎,已经阅读了多个线程并对此进行了讨论,但似乎找不到问题。我在我的应用程序中采用了与另一段代码相同的方法。
当我从源var
实例化dictionary
时,我为ViewController
ViewController
设置了一个值。
但是控制台继续输出包装为Optional的字符串,因此也在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
答案 0 :(得分:1)
如果您查看要打印值的行,则会注意到您正在尝试打印可选值。
print("sender.name: \(sender!.firstName), \(sender!.birthday), \(String(describing: sender?.occupation))")
在最后一部分中,您尚未解包sender
,它仍然是可选的,而属性变量occupation
也是可选的。用
print(sender?.occupation ?? "")
或
if let occupation = sender?.occupation {
print(occupation)
}
答案 1 :(得分:0)
因为您的var occupation: String?
是可选的。
String(describing:object)
与"\(object)"
相同,如果对象是Optional
类型,则文本将由Optional()
换行。您必须打开包装才能摆脱它。