如何从 Firebase 中获得节点的?现在,我使用此代码来获取,但无法从第一和第二个儿童来获取图像:

    var images = [String]()
    var images2 = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        nameAddressRef = Database.database().reference(withPath: "Студии")

        loadImages()
    }
    func loadImages() {
        nameAddressRef.observe(DataEventType.value, with: { (snapshot) in
            var newImage = [String]()
            var newImage1 = [String]()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let imageSnap = snap.childSnapshot(forPath: "Images")
                let dict = imageSnap.value as! [String: AnyObject]
                let url1 = dict["image1"] as! String
                let url2 = dict["image2"] as! String

                newImage.append(url1)
                newImage1.append(url2)

            }
            self.images = newImage
            self.images2 = newImage1
        })
    }
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CategoryRow

        cell.nameLabel.text = nameAddress[indexPath.section].name

        let array = [images[indexPath.section],
                      images2[indexPath.section]]
        cell.images = array.filter({ !($0.isEmpty) })

        return cell
    }
}

我了解到我的功能中缺少某些内容,但是我无法理解的是什么。
而我的firebase结构:
{
  "Студии" : {
    "Дубаи" : {
      "1" : {
        "Images" : {
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1",
          "name" : "qwerty"
        }
      },
      "2" : {
        "Images" : {
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1",
          "name" : "qwerty2"
        }
      },
      "Images" : {
        "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1"
      },
      "address" : "Тутушка",
      "name" : "Дубаи"
    }
  }
}

感谢帮助!

最佳答案

尝试这个:-

   func loadImages (){
    Database.database().reference(withPath:
   "Студии").child("Дубаи").observe(.value, with: { (snapShot) in
        if snapShot.exists() {
            let array:NSArray = snapShot.children.allObjects as NSArray

            for child in array {
                let snap = child as! DataSnapshot
                if snap.value is NSDictionary {
                    let data:NSDictionary = snap.value as! NSDictionary
                    if let dict = data.value(forKey: "Images") {
                        let dictImage:NSDictionary = dict as!
        NSDictionary
                        if let image  = dictImage["image1"] {
                            print(image)
                        }
                    }
                }

               // newImage1.append(url2)

            }
        }
    })
}

关于ios - 从Firebase获取子节点的子节点(Swift 3),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45861518/

10-12 04:12