【问题标题】:Ambiguous use of subscript下标使用不明确
【发布时间】:2016-05-18 15:31:58
【问题描述】:

这段代码可以正常工作,但现在不行了。我在 lat 和 long 变量上收到错误“'subscript' 的模糊使用”。这是怎么回事?这是因为 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)

}

【问题讨论】:

    标签: arrays json swift alamofire


    【解决方案1】:

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

    coordinates 向下转换为Array&lt;[Double]&gt;

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

    那么您无需进一步类型转换即可获取元素

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

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多