我遇到了这个错误,但我正在尝试从 Dictionary
中获取 String
。这是我的代码:
FIRDatabase.database().reference().child("users").child(uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
let dictionary = snapshot.value as! NSDictionary
if let username = dictionary["name"] as? String {
cell.name.text = username
}
if let userlogin = dictionary["login"] as? String {
cell.login.text = userlogin
}
})
在我的 Firebase 数据库 “name” 和 “login” 都是字符串。我不明白这是什么问题。
如有任何帮助,我们将不胜感激!
最佳答案
关于将快照转换到 NSDictionary 的问题。由于快照值是一个字符串。 试试这个:
FIRDatabase.database().reference().child("users").child(uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let dictionary = snapshot.value as? NSDictionary {
if let username = dictionary["name"] as? String {
cell.name.text = username
}
if let userlogin = dictionary["login"] as? String {
cell.login.text = userlogin
}
}
})
关于Swift - 无法将类型 '__NSCFString' 的值转换为 'NSDictionary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831641/