下标的模糊使用

时间:2016-02-08 20:26:27

标签: arrays json swift alamofire

这段代码正在运行,现在却没有。我收到错误"模糊地使用'下标'"关于lat和long变量。发生了什么事?这是因为Swift更新吗?

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)

}

1 个答案:

答案 0 :(得分:4)

coordinates被转换为AnyObject数组 编译器不知道它实际上是另一个数组中Double的数组。

向下转发coordinatesArray<[Double]>

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

然后你可以获得没有进一步类型转换的元素

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