let db = Firestore.firestore()
let docRef = db.collection("users").document(result!.user.uid)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: (dataDescription)")
print()
} else {
print("Document does not exist")
}
}
print("Document data: (dataDescription)")
outputs the following:
Document data: ["uid": LjqBXo41qMStt89ysQ4I9hxla2h1, "firstname": Tim, "lastname": Dorsemagen]
How can I extract each value such as the uid
, the firstname
and the lastname
from dataDescription
?
2
Answers
There are several ways to accomplish this. One of the most easily understood would be to break the string down in sections. Assuming you only want the values rather than their keys:
Firestore has everything needed to easily get components of a document. Here’s an asynchronous example of reading a users name from a document and returning it
if you want to use Codable (advised! See Mapping Firestore Data), this works for printing the name (can also be combined with the above solution)
or just a regular old read of a document and print the name
*note: no error checking and I am force unwrapping options. Don’t do that.