2017-08-24 105 views
0

這是我的實時數據庫結構。我在俱樂部只有一件物品。實際上,我有很多項目。如何從Firebase讀取數據

enter image description here

我想讀的所有俱樂部的信息,並嘗試使用俱樂部的鑰匙,以獲得相關的地址。

這裏是我的代碼:

func loadClubs() { 

     ref = Database.database().reference() 
     let clubRef = ref.child("club") 
     let refHandle = clubRef.observe(DataEventType.value, with: { (snapshot) in 
      if let c = snapshot.value as? [String : AnyObject] { 
       let name = c["name"] as! String // PRINT NIL 



      } 

      // ... 
     }) 
    } 

我怎樣才能檢索俱樂部的名字,courtNum,解釋,......?

回答

1

試試這個: -

ref = Database.database().reference() 

ref.child("club").observe(.value, with: { (snapshot) in 
print("clubs: \(snapshot)") 

if(snapshot.exists()) { 
    let array:NSArray = snapshot.children.allObjects as NSArray 

    for obj in array { 
     let snapshot:FIRDataSnapshot = obj as! FIRDataSnapshot 
     if let childSnapshot = snapshot.value as? [String : AnyObject] 
      { 
      if let clubName = childSnapshot["name"] as? String { 
       print(clubName) 
      } 
     } 
    } 

} 
}