首页 文章

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

提问于
浏览
6

我收到了这个错误,但是我想从 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 Database "name"和"login"都是字符串 . 我无法理解这是什么问题 .

任何帮助将不胜感激!

1 回答

  • 6

    问题将快照转换为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
                }
            }
        })
    

相关问题