简体   繁体   English

如何使用 swift 在 Firestore 文档的字段中生成 null 值?

[英]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.我想在 Firestore 文档中创建一个字段为 null,就像上图中的lastEventApproval字段一样。

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所以我想在成功创建文档时,有一个名为lastEventApproval的字段,其值为 null ,如上图

but the result is....但结果是……

that lastEventApproval doesn't exist if I use that code.如果我使用该代码,则lastEventApproval不存在。 like this像这样在此处输入图像描述

in Android, I can make something like that using this code在 Android 中,我可以使用此代码制作类似的东西

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?但我不知道为什么它在 iOS swift 中不起作用,也许我需要指定除 nil 以外的其他值?

so how to make a field in firestore document to be null in swift?那么如何使firestore文档中的字段成为swift中的null?

If you want to actually write a null to use as a placeholder (for example) it's just this如果您想实际编写一个 null 用作占位符(例如),就是这样

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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