LoginSignup
0
0

More than 1 year has passed since last update.

【Swift】【Firestore】Firestore.Encoder().encode() で nil の key が消えない様にする

Posted at

GitHub の gg-mark/NullCodable にある NullCodable.swift をコピーしておき、 encode したい構造体の Optional 型のプロパティに @NullCodableという propertyWrapper を付ければ消えない様になります。

import FirebaseFirestore

struct Data: Codable {
    var a: Int? = nil
    @NullCodable var b: Int? = nil
}

let data = Data()
let encodedData = Firestore.Encoder().encode(data)
print(encodedData)
["b": <null>]

NullCodable がない a は消えて、
NullCodable をつけた b は残っている。

参考
Codableでnullが消えてしまう問題に対応する _ えいむーさんは明日も頑張るよ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0