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)
}
})
}
Aktualisieren Sie Ihre Q mit minimaler JSON Struktur als Text und Ihre vollständige Funktion – Dravidian
'lassen getLatitude = locationDict [ "latitide"] wie? Double' –
@Dravidian Ich habe die Funktion hinzugefügt und ich benutze Firebase. – Noah