快速检查以查看firebase值是true还是false

时间:2016-06-20 19:09:34

标签: swift firebase

您好我正在从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

            */

        }

    })

}

firebase json enter image description here

1 个答案:

答案 0 :(得分:0)

根据Firebase Docsvar 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
        }

    })

}