您好我正在从firebase中检索一些信息,我正在寻找的数据是三个孩子深,并且包含一个布尔值。当我打印的值能够得到结果但我无法看到结果是真还是假。
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
*/
}
})
}
答案 0 :(得分:0)
根据Firebase Docs,
var n = 5;
var lineOfStars = '';
for(var i = 1; i <= n; i++){
for(var j = 1; j <= n; j++){
if(j <= n - i + 1){
lineOfStars += '*' ;
} else{
lineOfStars += j ;
}
}
lineOfStars += '\n' ;
}
console.log(lineOfStars);
是。 。 。
此快照中子节点的快照的迭代器。您可以使用本机
.children
语法。
。 。 。然后,您必须使用for..in
访问其数据,并使用.value
访问其密钥。
因此,你可以这样做。
.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
}
})
}