Within our app written in Swift and using SwiftUI, we needed a way to read our data from Google Firestore into the app, and then write some data back into Firestore.

Initially, I followed this guide from the Firestore documentation, although it involved a lot of mapping dictionary key/values to the type I was after. Writing data was much the same. For each property in my Swift class I had to specify the name of the corresponding Firestore field, and then assign the appropriate value to the field. I knew there must be a better way.

Fortunately, the Firestore documentation recognises this problem and offers a way to bring your document into a Swift class, and allow you to use it as you would any other piece of data. This is through the use of the Pod FirebaseFirestoreSwift , which adds an extension method to the document.data() call so you can specify what type the data is coming in as. There is also a similar method on the document.setData() method for converting your Swift object back to a Firestore object.

#ios #firestore #swift #enums

Firestore objects for Swift Struct and Back, Enums and Arrays
7.55 GEEK