简体   繁体   中英

how to make null value in a field in firestore document using swift?

在此处输入图像描述

I want to make a field in firestore document to be null like lastEventApproval field in the image above.

I try to create a document by using the code below

let data = ["lastEventApproval" : nil , other data here .....]
batch.setData(userDictionary, forDocument: ref)

so I want to make when I successfully create a document, there is a field called lastEventApproval and the value is null like the image above

but the result is....

that lastEventApproval doesn't exist if I use that code. like this在此处输入图像描述

in Android, I can make something like that using this code

val data = hashMapOf("lastEventApproval" to null)
batch.set(ref,data)

but I don't know why it doesn't work in iOS swift, maybe I need to asign other value than nil?

so how to make a field in firestore document to be null in swift?

If you want to actually write a null to use as a placeholder (for example) it's just this

func writeNull() {
    let collRef = self.db.collection("test_collection")
    let data: [String: Any] = [
        "key": "value",
        "key1": NSNull()
    ]
    collRef.addDocument(data: data)
}

and the result

在此处输入图像描述

This also works

func writeNull() {
    let collRef = self.db.collection("test_collection")
    let data: [String: Any?] = [
        "key": "value",
        "key1": nil
    ]
    collRef.addDocument(data: data)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM