我使用的函数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)
}
})
答案 0 :(得分:2)
指定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)
}
})
或者使用尾随闭包语法(参见 Swift编程语言的Closures章节中的尾随闭包部分),你可以在其中拉出闭包(
和)
并将其放在)
:
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)
}
}
答案 1 :(得分:1)
在完成处理程序之前添加参数。
geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject], error: NSError) -> Void in // Added 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)
}
})