Swift - 无法将'__ NSCFString'类型的值转换为'NSDictionary'

Ego*_*sov 7 firebase swift firebase-realtime-database

我收到了这个错误,但我正试图从中得到String一个Dictionary.这是我的代码:

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
            }

        })
Run Code Online (Sandbox Code Playgroud)

在我的Firebase数据库中, "name""login"都是字符串.我无法理解这是什么问题.

任何帮助将不胜感激!

lub*_*lis 7

问题将快照转换为NSDictionary.由于快照值是String.试试这个:

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
            }
        }
    })
Run Code Online (Sandbox Code Playgroud)