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
struct Book { | |
var isbn: String | |
var title: String | |
var authors: [String] | |
var year: Int | |
var country: String | |
var createdAt: Date | |
var format: Format | |
var award: String? | |
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
struct FirestoreBook { | |
var isbn: String | |
var title: String | |
var authors: [String] | |
var year: Int | |
var country: String | |
var createdAt: Date | |
var format: Format | |
var award: String? | |
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
let book = FirestoreBook( | |
isbn: "95-0443-843-0", | |
title: "Do Androids Dream of Electric Sheep?", | |
authors: ["Philip K. Dick"], | |
year: 1968, | |
country: "United States", | |
createdAt: Date(), | |
format: .ebook, | |
award: nil) |
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
let firestore = Firestore.firestore() | |
let ref = firestore.document("books/95-0443-843-0") | |
ref.getModel(FirestoreBook.self) { book, error in | |
guard let book = book else { return } | |
print("The book \(book.title) was published in \(book.year).") | |
} |
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
struct FirestoreBook { | |
let documentID: String! | |
var isbn: String | |
var title: String | |
} | |
extension FirestoreBook: FirestoreModel { | |
init?(modelData: FirestoreModelData) { | |
try? self.init( |
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
let book = FirestoreBook( | |
documentID: nil, | |
isbn: "95-0443-843-0", | |
title: "Do Androids Dream of Electric Sheep?") | |
let firestore = Firestore.firestore() | |
let ref = firestore.collection("books").document() | |
ref.setModel(book) |
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
import FirebaseFirestore | |
private struct Property { | |
let label: String | |
let value: Any | |
} | |
struct FirestoreModelData { | |
let snapshot: DocumentSnapshot |
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
struct Book: Codable { | |
var isbn: String | |
var title: String | |
var authors: [String] | |
var year: Int | |
} |
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
let json = """ | |
{ | |
"isbn": "95-0443-843-0", | |
"title": "Do Androids Dream of Electric Sheep?", | |
"authors": ["Philip K. Dick"], | |
"year": 1968, | |
"country": "United States" | |
} | |
""".data(using: .utf8)! |
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
import Foundation | |
enum PathParamsError: Error { | |
case unbalanced | |
case nonExistentParam(key: String) | |
case invalidConversion(value: String, type: String) | |
var localizedDescription: String { | |
switch self { | |
case .unbalanced: |