Beth Knight February 2016

Ambiguous use of subscript

This block of code was working and now it's not. I get the error "Ambiguous use of 'subscript'" on the lat and long variables. What's going on? Is this because of a Swift update?

func showPrecincts() {

    var urlBoundaries = "http://www.oklahomadata.org/boundary/1.0/boundary/?contains=" + "\(coords!.latitude)" + "," + "\(coords!.longitude)" + "&sets=precincts"

    Alamofire.request(.GET, urlBoundaries, parameters: ["foo": "bar"])
        .responseJSON { response in

            if let data = response.result.value {

                let nestedCoordinates = data.valueForKeyPath("objects.simple_shape.coordinates") as! Array<AnyObject>

                let bug1 = nestedCoordinates.first as! Array<AnyObject>
                let bug2 = bug1.first as! Array<AnyObject>
                let coordinates = bug2.first as! Array<AnyObject>

                var convertedCoords: [CLLocationCoordinate2D] = []

                for individualCoordinates in coordinates  {
                    let lat = (individualCoordinates[1] as! Double)
                    let long = (individualCoordinates[0] as! Double)
                    var newCoords = CLLocationCoordinate2DMake(lat, long)
                    convertedCoords.append(newCoords)
                }
                print(convertedCoords)

}

Answers


vadian February 2016

coordinates is casted to an array of AnyObject.
The compiler does not know that it's actually an array of Double in another array.

Downcast coordinates to Array<[Double]>

let coordinates = bug2.first as! Array<[Double]>

then you can get the elements without further type casting

let lat = individualCoordinates[1]
let long = individualCoordinates[0]

Post Status

Asked in February 2016
Viewed 2,834 times
Voted 11
Answered 1 times

Search




Leave an answer