Swift-无法转换类型' __ NSCFString'的值到' NSDictionary' [英] Swift - Could not cast value of type '__NSCFString' to 'NSDictionary'
本文介绍了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
}
}
})
这篇关于Swift-无法转换类型' __ NSCFString'的值到' NSDictionary'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文