Sam
Sam

Reputation: 137

swift 3 firebase current user query

I'm new to firebase i'm trying to retrive current user info, which is username, photo, etc.

Here is the code:

let queryRef = FIRDatabase.database().reference().child("users")


    queryRef.child(FIRAuth.auth()!.currentUser!.uid).observe(.value, with: { (snapshot) -> Void in
        print(snapshot.value as! NSDictionary)
    })

Here is output

{

email = "[email protected]";

gender = male;

nickname = "Sam <3";

username = sam;

uid = 4nyyBF91JhSLY0DhkjfJ0DCDZK03;

urlToImage = "https://firebasestorage...;

}


And how can i print something specific like getting email, username, nickname as String, And photo as uiimage?

Upvotes: 0

Views: 250

Answers (1)

Nirav D
Nirav D

Reputation: 72410

You just need to simply use subscript with your Dictionary. Also in Swift use Dictionary instead of NSDictionary.

let queryRef = FIRDatabase.database().reference().child("users")
queryRef.child(FIRAuth.auth()!.currentUser!.uid).observe(.value, with: { (snapshot) -> Void in

    if let dictionary = snapshot.value as? [String:Any] {
        let email = dictionary["email"] as? String ?? ""
        print(email)
        let gender = dictionary["gender"] as? String ?? ""
        print(gender)
        //Access the other key same way.
    }
})

Upvotes: 1

Related Questions