1

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)

        }
    })

}
adjuremods
  • 2,938
  • 2
  • 12
  • 17
Noah-1
  • 396
  • 1
  • 5
  • 20

2 Answers2

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

    }
Dravidian
  • 9,945
  • 3
  • 34
  • 74
0

Things get substantially easier if you cast to a type-safe dictionary, e.g.:

snapshot.value! as! [String:Any]

For a slightly larger example, see the code from this answer I wrote earlier today:

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"]!)")
    }
})
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807