Noah 1 Noah 1 - 1 month ago 19
Swift Question

How to access a dictionary value with Swift 3?

So since the release of Swift 3, a part of my code where I access a dictionary isn't working anymore, here is the code with the previous release of swift:

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


Now with the new release I'm not sure how to rewrite "getLocation". I only rewrote the last two lines with the new syntax:

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


I am using Firebase, this is the complete function: (it adds an array of annotations to a map)

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)

}
})

}

Answer

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

    }