无法转换类型的值'([AnyObject] !, NSError!) - >虚空'期望参数类型' CLGeocodeCompletionHandler'

时间:2016-07-11 08:02:29

标签: ios swift swift2 core-location

我想跳过使用Xcode 7.3.1并转换我的代码,但是我在这里遇到了一些问题,这就是我以前在Swift 1.1中使用它的方法,但是我收到了错误:< / p>

  

无法转换类型&#39;([AnyObject]!,NSError!)的值 - &gt;虚空&#39;期望参数类型&#39; CLGeocodeCompletionHandler&#39; (又名&#39;(可选&gt;,可选) - &gt;()&#39;):

这是我的代码:

  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)")                    
            }
        }
    })
}

1 个答案:

答案 0 :(得分:1)

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

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

})