2017-08-24 21 views
1

どのように私は子供子のfirebaseからノードが入手できますか?今、私は子供を取得するには、このコードを使用しますが、私は第一および第二の子供から画像取得することはできません。firebaseから子ノードの子を取得します(SWIFT 3)

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 
    } 
} 

を私はFUNCで何かが欠けているが、私は何をしないのですということを理解します私は理解できない。 そして、私の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" : "Дубаи" 
    } 
    } 
} 

ありがとう!

+0

をImages'? – theblindprophet

答えて

2

はこれを試してください: -

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) 

      } 
     } 
    }) 
} 
+0

ありがとうございました。しかし、私はいくつかのパスから取得したい場合、どのように私はそれを作ることができますか?たとえば、私は "Дубаи"、 "Лондон"、 "Москва"を持ちます。 – onpro

1

ループが一致しないようにСтудии下にはImagesは、ありません。

あなたはJSONの下位1つのレベル聞きたいでしょう: `てみましょうimageSnap = snap.childSnapshot(forPath: "イメージ") '1'`と ` - >あなたはこれを繰り返すことができ

nameAddressRef = Database.database().reference(withPath: "Студии/Дубаи") 
関連する問題