繁体   English   中英

获得Firebase儿童价值

[英]Acessing Firebase Child Value

我需要在UID之后访问每个孩子:

JSON

Json2

我需要获取+ vvvv值。 并将这些值放在我的数组中,以便tableView单元格加载所有值*。

在调用firebase时,只需要帮助如何获得这些值。

例如:

+message_view
   +UID
      +value1
      +value2
      +value3

我需要访问这些值并将它们放入我的tableView单元格中。

 override func viewDidLoad() {
        super.viewDidLoad()

        let userID = FIRAuth.auth()?.currentUser?.uid

        if let userId = userID {
            ref.child("message_view").child(userId).observeSingleEvent(of: .value, with: {(snapshot) in

            let snapDict = snapshot.value as? NSDictionary




            //Assign array values
            self.messageDataArray.insert(postStruct(message: theValueIwant), at: 0)


            //self.tableView.reloadData()


        })

假设您的UID下的值是字符串,您可以执行以下操作。 如果它们不是字符串,只需更改数据数组的类型并转换为正确的类型。

override func viewDidLoad() {
    super.viewDidLoad()

    let userID = FIRAuth.auth()?.currentUser?.uid

    if let userId = userID {
        ref.child("message_view").child(userId)
           .observeSingleEvent(of: .value, 
                               with: {(snapshot) in
            var dataArray = [String]()
            for child in snapshot.children {
                let childSnapshot = child as! FIRDataSnapshot
                dataArray.append(child.value as! String)
            }

            //do anything with dataArray
        })
    }
}

一旦for child in snapshot.children循环中for child in snapshot.children运行完毕,你就可以将你UID的所有子节点作为一个数组,并且可以随心所欲地执行任何操作。

暂无
暂无

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

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