mirror of
https://github.com/caoer/CodableFirebase.git
synced 2026-04-18 12:06:40 +08:00
41 lines
1.3 KiB
Markdown
41 lines
1.3 KiB
Markdown
# CodableFirebase
|
|
Use Codable with Firebase
|
|
|
|
[](https://github.com/Carthage/Carthage)
|
|
[](https://travis-ci.org/alickbass/CodableFirebase)
|
|
[](https://codecov.io/gh/alickbass/CodableFirebase)
|
|
|
|
## Overview
|
|
|
|
This library helps you to use your custom type that conform to `Codable` protocol with Firebase. Here's an example of model:
|
|
|
|
```swift
|
|
struct Model: Codable {
|
|
let stringExample: String
|
|
let booleanExample: Bool
|
|
let numberExample: Double
|
|
let dateExample: Date
|
|
let arrayExample: [String]
|
|
let nullExample: Int?
|
|
let objectExample: [String: String]
|
|
}
|
|
```
|
|
|
|
And this is how you would encode it with [Firebase Firestore](https://firebase.google.com/products/firestore/):
|
|
|
|
```swift
|
|
import Firebase
|
|
|
|
let model: Model // here you will create an instance of Model
|
|
do {
|
|
let docData = try FirestoreEncoder().encode(model)
|
|
Firestore.firestore().collection("data").document("one").setData(docData) { err in
|
|
if let err = err {
|
|
print("Error writing document: \(err)")
|
|
} else {
|
|
print("Document successfully written!")
|
|
}
|
|
}
|
|
}
|
|
```
|