Created
July 6, 2020 13:29
-
-
Save lanserxt/31b969c34ba795cc72f17553e881d059 to your computer and use it in GitHub Desktop.
Firebase document decoding to Swift Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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