2016-10-17 5 views
1

So seit der Veröffentlichung von Swift 3, ein Teil meines Codes, wo ich einen Wörterbuch Zugriff funktioniert nicht mehr, hier ist der Code mit der vorherige Version von swift:Wie kann mit Swift 3 auf einen Wörterbuchwert zugegriffen werden?

var locationDict: NSDictionary?//location dictionary 
if let getLocation = item.value?["Location"]{locationDict = getLocation as? NSDictionary} 

//get dictionary values 
let getLatitude = locationDict?.valueForKey("latitude") as! Double 
let getLongitude = locationDict?.valueForKey("longitude") as! Double 

Jetzt mit dem neuen Release I bin mir nicht sicher, wie ich "getLocation" umschreiben soll. Ich schrieb nur die letzten beiden Zeilen mit der neuen Syntax:

//get dictionary values 
let getLatitude = locationDict?.value(forKey: "latitude") as! Double 
let getLongitude = locationDict?.value(forKey: "longitude") as! 

I Firebase verwenden, das die komplette Funktion: (es eine Reihe von Anmerkungen zu einer Karte fügt)

func setAnnotations(){ 

    //get data 
    ref.child("Stores").observe(.value, with: { (snapshot) in 

     self.mapView.removeAnnotations(self.annArray) 

     for item in snapshot.children { 

      let annotation = CustomAnnotation() 

      //set all data on the annotation 
      annotation.subtitle = (snapshot.value as? NSDictionary)? ["Category"] as? String 
      annotation.title = (snapshot.value as? NSDictionary)? ["Name"] as? String 
      annotation.annImg = (snapshot.value as? NSDictionary)? ["Logo"] as? String 

      var locationDict: NSDictionary?//location dictionary 
      if let getLocation = item.value?["Location"]{locationDict = getLocation as? NSDictionary} 


      let getLatitude = locationDict?.value(forKey: "latitude") as! Double 
      let getLongitude = locationDict?.value(forKey: "longitude") as! Double 

      annotation.coordinate = CLLocationCoordinate2D(latitude: getLatitude, longitude: getLongitude) 


      self.annArray.append(annotation) 

      self.mapView.addAnnotation(annotation) 

     } 
    }) 

} 
+0

Aktualisieren Sie Ihre Q mit minimaler JSON Struktur als Text und Ihre vollständige Funktion – Dravidian

+0

'lassen getLatitude = locationDict [ "latitide"] wie? Double' –

+0

@Dravidian Ich habe die Funktion hinzugefügt und ich benutze Firebase. – Noah

Antwort

5

Try this: -

func setAnnotations(){ 

     //get data 
     FIRDatabase.database().reference().child("Stores").observe(.value, with: { (snapshot) in 

      self.mapView.removeAnnotations(self.annArray) 

      for item in snapshot.children{ 

       if let itemDict = (item as! FIRDataSnapshot).value as? [String:AnyObject]{ 

        annotation.subtitle = itemDict["Category"] as! String 
        annotation.title = itemDict["Name"] as! String 
        annotation.annImg = itemDict["Logo"] as! String 
        if let locationDict = itemDict["Location"] as? [String:AnyObject]{ 

         let getLatitude = locationDict["latitude"] as! Double 
         let getLongitude = locationDict["longitude"] as! Double 

         annotation.coordinate = CLLocationCoordinate2D(latitude: getLatitude, longitude: getLongitude) 
         self.annArray.append(annotation) 

         self.mapView.addAnnotation(annotation) 
        } 
       } 
      } 
     }) 

    } 
+0

genau was ich brauchte, danke! – Noah

0

Die Dinge wesentlich einfacher, wenn Sie auf einen typsicheren Wörterbuch werfen, zum Beispiel:

snapshot.value! as! [String:Any] 

Für ein etwas größeres Beispiel den Code aus this answer I wrote earlier today sehen:

012.351.
ref!.observe(.value, with: { (snapshot) in 
    for child in snapshot.children { 
     let msg = child as! FIRDataSnapshot 
     print("\(msg.key): \(msg.value!)") 
     let val = msg.value! as! [String:Any] 
     print("\(val["name"]!): \(val["message"]!)") 
    } 
}) 
Verwandte Themen