Swift - 无法将类型'__NSCFString'的值转换为'NSDictionary' [英] Swift - Could not cast value of type '__NSCFString' to 'NSDictionary'

查看:545
本文介绍了Swift - 无法将类型'__NSCFString'的值转换为'NSDictionary'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个错误,但是我试图从 Dictionary 获取 String 。这是我的代码:



$ p $ FIRDatabase.database()。reference()。child(users)。child(uid) .observeEventType(.ChildAdded,withBlock:{(snapshot)in

let dictionary = snapshot.value as!NSDictionary

如果让username = dictionary [name] as?如果让userlogin = dictionary [login] as?String {
cell.name.text = username
}

cell.login.text = userlogin

$ b})

Firebase数据库 namelogin都是字符串。我不明白是什么问题。

任何帮助将不胜感激!

解决方案试试这个:
$ b $ $ $ $ $ p $ FIRDatabase.database()。reference()。child(users)。child uid).observeEventType(.ChildAdded,withBlock:{(snapshot)in

如果让dictionary = snapshot.value as?NSDictionary {

如果let username = dictionary [name ] as?String {
cell.name.text = username
}

如果让userlogin = dictionary [login] as?String {
cell。 login.text = userlogin
}
}
})


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
            }

        })

In my Firebase Database "name" and "login" are both Strings. I cannot understand what's the problem.

Any help would be greatly appreciated!

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆