Beth Knight
Beth Knight

Reputation: 187

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)

}

Upvotes: 2

Views: 336

Answers (1)

vadian
vadian

Reputation: 285150

coordinates is cast 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]

Upvotes: 4

Related Questions