【问题标题】:Cannot convert value of type '([AnyObject]!, NSError!) -> Void' to expect argument type 'CLGeocodeCompletionHandler'无法将类型“([AnyObject]!,NSError!)-> Void”的值转换为期望参数类型“CLGeocodeCompletionHandler”
【发布时间】:2016-07-11 08:02:29
【问题描述】:

我想跳过去使用 Xcode 7.3.1 并转换我的代码,但我在这里遇到了一些问题,这就是我过去在 Swift 1.1 中使用它的方式,但我遇到了错误:

无法将类型“([AnyObject]!,NSError!)-> Void”的值转换为期望参数类型“CLGeocodeCompletionHandler”(又名“(可选>,可选)->()”):

这是我的代码:

  private func geoCodeAddress(address:NSString){

    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address as String, completionHandler: {(place marks: [AnyObject]!, error: NSError!) -> Void in ---> Error //Cannot convert value of type '([AnyObject]!, NSError!) -> Void' to expect argument type 'CLGeocodeCompletionHandler' (aka '(Optional<Array<CLPlacemark>>, Optional<NSError>)-> ()')

        if (error != nil) {                
            self.geocodingCompletionHandler!(gecodeInfo:nil,placemark:nil,error: error.localizedDescription)                
        }
        else{

            if let placemark = placemarks?[0] as? CLPlacemark {

                var address = AddressParser()
                address.parseAppleLocationData(placemark)
                let addressDict = address.getAddressDictionary()
                self.geocodingCompletionHandler!(gecodeInfo: addressDict,placemark:placemark,error: nil)
            }
            else {
                self.geocodingCompletionHandler!(gecodeInfo: nil,placemark:nil,error: "invalid address: \(address)")                    
            }
        }
    })
}

【问题讨论】:

    标签: ios swift swift2 core-location


    【解决方案1】:

    如错误消息中所述,CLGeocodeCompletionHandler 返回可选的不是具体对象,因此只需将 completionHandler 代码更改为

    geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
    
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-14
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多