Swift-无法转换类型' __ NSCFString'的值到' NSDictionary'
问题描述:
我收到此错误,但是我试图从 Dictionary
中获取 String
.这是我的代码:
I got this error, but I'm trying to get a String
from a Dictionary
. This is my code:
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数据库中,名称" 和登录" 都是字符串.我不明白是什么问题.
In my Firebase Database "name" and "login" are both Strings. I cannot understand what's the problem.
任何帮助将不胜感激!
答
问题将快照转换为NSDictionary.由于快照值是一个字符串.试试这个:
Issue regards snapshot cast to NSDictionary. Since snapshot value is a String. Try this:
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
}
}
})