gpt4 book ai didi

快速检查 firebase 值是真还是假

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:35 25 4
gpt4 key购买 nike

您好,我正在从 firebase 检索一些信息,我正在寻找的数据是三个子级深并且包含一个 bool 值。当我打印值时,我能够得到结果,但我无法查看结果是真还是假。

func userInfo() {

let dbRef = FIRDatabase.database().reference()
let stRef = FIRStorage.storage().reference()

dbRef.child("users/\(userBiD!)").observeEventType(.Value, withBlock: { snapshot in

let name = snapshot.value!["firstname"] as! String
let profileImage = snapshot.value!["profilePic"] as! String
let receivePostRequest = snapshot.value?["receivePostRequest"]

self.nameLabel.text = name
self.dbUserProfilePic(self.profilePhotoImageView, imageLink: profileImage)

let receivePost = snapshot.childSnapshotForPath("receivePostRequest")

let tags = receivePost.childSnapshotForPath("tags")

for child in tags.children {

/*
...
tags{
tag1 = true
tag2 = false

}
//I need to print all keys that have a true value

*/

}

})

}

火力地堡JSON enter image description here

最佳答案

因为,根据Firebase Docs ,.children 是 . . .

An iterator for snapshots of the child nodes in this snapshot. You can use the native for..in syntax.

. . .那么您必须使用 .value 来访问他们的数据,并使用 .key 来访问他们的 key 。

因此,您可以这样做。

func userInfo() {

let dbRef = FIRDatabase.database().reference()
let stRef = FIRStorage.storage().reference()

dbRef.child("users/\(userBiD!)").observeEventType(.Value, withBlock: { snapshot in

let name = snapshot.value!["firstname"] as! String
let profileImage = snapshot.value!["profilePic"] as! String
let receivePostRequest = snapshot.value?["receivePostRequest"]

self.nameLabel.text = name
self.dbUserProfilePic(self.profilePhotoImageView, imageLink: profileImage)

let receivePost = snapshot.childSnapshotForPath("receivePostRequest")

let tags = receivePost.childSnapshotForPath("tags")

for child in tags.children {
// NEW
if child.value == true {
print(child.key)
}
// NEW
}

})

}

或者,如果您想将所有真值保存在一个数组中。

func userInfo() {

let dbRef = FIRDatabase.database().reference()
let stRef = FIRStorage.storage().reference()

dbRef.child("users/\(userBiD!)").observeEventType(.Value, withBlock: { snapshot in

let name = snapshot.value!["firstname"] as! String
let profileImage = snapshot.value!["profilePic"] as! String
let receivePostRequest = snapshot.value?["receivePostRequest"]

self.nameLabel.text = name
self.dbUserProfilePic(self.profilePhotoImageView, imageLink: profileImage)

let receivePost = snapshot.childSnapshotForPath("receivePostRequest")

let tags = receivePost.childSnapshotForPath("tags")

for child in tags.children {
// NEW
var trueValues: [Bool] = []

if child.value == true {
trueValues.append(child.key)
}
// NEW
}

})

}

关于快速检查 firebase 值是真还是假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com