I am new to swift and firebase, and I am wondering why I receive snapshot.value as? NSDictionary as nil even though the user is signed in.
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot.value as? NSDictionary)
if let value = snapshot.value as? NSDictionary{
self.title = value["firstname"] as? String
print("o")
}
print(self.title)
}, withCancel: { (error) in
print(error.localizedDescription)
})
}
//I want it to return the name of the user (Ben) as the title of the navigation bar, but instead it returns nil
Database is:
{
"users" : {
"-MBzED86cdVbvBNcYdpY" : {
"email" : "[email protected]",
"firstname" : "benjamin",
"username" : "bb"
},
"-MBzEWEQ3cX6d5aJOByR" : {
"email" : "[email protected]",
"firstname" : "bslou",
"username" : "bbbslou.row"
}
}
}
My program returns "Snap (mDNGl4fQXEMx1wPnjGuhILp5f8j1) " when I print snapshot
print(type(of: snapshot.value))
show? And avoid to useNS...
collection types in Swift. – vadianFIRDataSnapshot
, ok I will change to Dictionary – Benjamin Sloutsky