gpt4 book ai didi

swift - 调用中参数 'completionHandler' 缺少参数

转载 作者:可可西里 更新时间:2023-11-01 01:05:31 24 4
gpt4 key购买 nike

我使用了 geocodeAddressString 函数,它在 swift 1.0 中有效,但在 swift2 中无效。谁能告诉我我的代码有什么问题以及如何解决这个问题?谢谢!

geocoder.geocodeAddressString(address, {(placemarks: [AnyObject], error: NSError) -> Void in  //Error: Missing argument for parameter 'completionHandler' in call

if let placemark = placemarks?[0] as? CLPlacemark {
let annotation = MKPointAnnotation()
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
annotation.coordinate = location
annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
annotation.subtitle = "\(StudentArray[student].grade)"
self.mapView.addAnnotation(annotation)
}

})

最佳答案

要么指定completionHandler参数名:

geocoder.geocodeAddressString(address, completionHandler: { placemarks, error in
if let placemark = placemarks.first as? CLPlacemark {
let annotation = MKPointAnnotation()
let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
annotation.coordinate = location
annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
annotation.subtitle = "\(StudentArray[student].grade)"
self.mapView.addAnnotation(annotation)
}
})

或者使用尾随闭包语法(参见 The Swift Programming LanguageClosures 章的 Trailing Closure 部分),您可以在其中将闭包从 () 并将其放在 ) 之后:

geocoder.geocodeAddressString(address) { placemarks, error in
if let placemark = placemarks.first as? CLPlacemark {
let annotation = MKPointAnnotation()
let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
annotation.coordinate = location
annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
annotation.subtitle = "\(StudentArray[student].grade)"
self.mapView.addAnnotation(annotation)
}
}

关于swift - 调用中参数 'completionHandler' 缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31590464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com