Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 仅在一个级别创建一个随机关键点_Json_Swift_Firebase - Fatal编程技术网

Json 仅在一个级别创建一个随机关键点

Json 仅在一个级别创建一个随机关键点,json,swift,firebase,Json,Swift,Firebase,在Firebase中,可以使用.childByAutoId()创建随机键 这将产生如下json结构: { "category" : { "random-key-generated-by-Firebase" : { "someValue" : true } } } 我想知道有没有可能跳过最后一层,而得到这样一个结构 { "category" : { "random-key-generated-by-Firebase" : true } } 对

在Firebase中,可以使用.childByAutoId()创建随机键

这将产生如下json结构:

{
  "category" : {
    "random-key-generated-by-Firebase" : {
      "someValue" : true
    }
  }
}
我想知道有没有可能跳过最后一层,而得到这样一个结构

{
  "category" : {
    "random-key-generated-by-Firebase" : true
  }
}

对于swift,首先创建一个键引用

let key = ref.childByAutoId().key
然后创建一个childUpdate容器

let childUpdates = ["/category/\(key)": true]
最后更新

ref.updateChildValues(childUpdates)

对于swift,首先创建一个键引用

let key = ref.childByAutoId().key
然后创建一个childUpdate容器

let childUpdates = ["/category/\(key)": true]
最后更新

ref.updateChildValues(childUpdates)

我对firebase不太了解,但请尝试使用这个
newEntry.setValue(true)
。setValue应该是一个简单的值,而不是数组。下面的FBRef.child(“category”).childByAutoId().setValue(true)将创建您需要的结构mentioned@EdmundElmer您提到的“数组”实际上是字典的Swift语法,它是
setValue()
的有效参数。但是您的回答是正确的,
setValue(true)
将产生Shane请求的结构。@FrankvanPuffelen:对不起,当然,这是一本字典。我使用(不正确)“array”作为包含多个值的变量的同义词。我对firebase不太了解,但请尝试使用此
newEntry.setValue(true)
。setValue应该是一个简单的值,而不是数组。下面的FBRef.child(“category”).childByAutoId().setValue(true)将创建您需要的结构mentioned@EdmundElmer您提到的“数组”实际上是字典的Swift语法,它是
setValue()
的有效参数。但是您的回答是正确的,
setValue(true)
将产生Shane请求的结构。@FrankvanPuffelen:对不起,当然,这是一本字典。我使用(不正确地)“array”作为包含多个值的变量的同义词。