swift - Is there anyway to access only the value of a dictionary in a child? -


in firebase database swift, there way access value (as string) dictionary. each child has key:value pair. example:

users |-- bob     |-- bob@gmail.com: "bobbbby" 

is there way access "bobbbby" string? new swift appreciated.

example firebase database docs

taken directly https://firebase.google.com/docs/database/ios/read-and-write

let userid = auth.auth().currentuser?.uid ref.child("users").child(userid!).observesingleevent(of: .value, with: { (snapshot) in     // user value     let value = snapshot.value as? nsdictionary     let username = value?["username"] as? string ?? "" // <<< line     let user = user.init(username: username)      // ...     }) { (error) in         print(error.localizeddescription) } 

updated you

if need multiple fields under user:

ref.child("users").child("bob").observesingleevent(of: .value, with: { (snapshot) in     let value = snapshot.value as? nsdictionary     let emailvalue = value?["bob@gmail.com"] as? string ?? ""     // more... } 

...or if need 1 value:

ref.child("users").child("bob").child("bob@gmail.com").observesingleevent(of: .value, with: { (snapshot) in     if let value = snapshot.value as? string {         // use value...     } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -