2017-02-28 127 views
1

我對swift和Firebase相當陌生。我有這樣的數據庫:My database previewSwift 3中的Firebase數據庫設置和數據處理

在應用程序中我有多個註釋,我需要將信息從數據庫傳遞給他們。在這一點上,我對將數據作爲字典讀取並傳遞給註釋感到困惑。 這是我以前的代碼時,我沒有使用數據庫,使用數組:

for i in 0...2 
    { 
     let coordinate = coordinates[i] 
     let point = myAnnotation(coordinate: CLLocationCoordinate2D(latitude: coordinate[0] , longitude: coordinate[1])) 
     point.name = names[i] 
     point.address = addresses[i] 
     point.hours = hours[i] 
     point.phones = phones[i] 

     self.mapView.addAnnotation(point) 
    } 

你不必爲我的代碼,但我至少需要一些提示吧。

+0

顯示您正在嘗試的Firebase基本代碼。 –

回答

1

我有我的火力點一個類似的數據庫,我會做這樣的:

FIRDatabase.database().reference().child("Data").observe(.value, with: {(snapshot) in 
    if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] { 
     for snap in snapshot{ 
      if let dict = snap.value as? [String: Any] { 
       if let address = dict["address"] as? String,let lat = dict["lat"] as? String, let long = dict["long"] as? String, let name = dict["name"] as? String { 
        let coordinate = coordinates[i] 
        let point = myAnnotation(coordinate: CLLocationCoordinate2D(latitude: lat , longitude: long)) 
        point.name = name 
        point.address = address 

        self.mapView.addAnnotation(point) 
       } 
      } 
     } 
    } 
}) 

,我環路爲單元值作爲字典的數據節點,合格的每一個孩子,然後讀字典和類型將其值轉換爲格式是否正確,然後將其賦值並將其附加到數組。

+1

是的,這正是我想要做的,你救了我的一天。還有2個小錯誤1,Lat和Long必須是「as Double」,2,只需刪除「let coordinate = coordinates [i]」這一行,非常感謝你的幫助。 –

+0

很高興幫助:D是啊我只是用字符串,這就是爲什麼我沒有意識到你在哪裏使用雙打。 –