Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Could not cast value of type '__NSCFString' to 'NSDictionary'

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!

like image 900
Egor Kuznetsov Avatar asked Aug 08 '16 14:08

Egor Kuznetsov


1 Answers

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
            }
        }
    })
like image 84
lubilis Avatar answered Feb 23 '23 03:02

lubilis