Skip to content

Instantly share code, notes, and snippets.

@lanserxt
Created July 6, 2020 13:29
Show Gist options
  • Save lanserxt/31b969c34ba795cc72f17553e881d059 to your computer and use it in GitHub Desktop.
Save lanserxt/31b969c34ba795cc72f17553e881d059 to your computer and use it in GitHub Desktop.
Firebase document decoding to Swift Object
func resolveFirebaseObject<T: Codable>(_ snapshot: DocumentSnapshot) -> T? {
guard snapshot.exists else {return nil}
if let data = try? JSONSerialization.data(withJSONObject: snapshot.data(), options: []) {
do {
return try self.decoder.decode(T.self, from: data)
} catch {
print("Decoding error: \(error)")
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment