ViewController 不符合协议 GMSAutoCompleteViewControllerDelegate in Swift
ViewController does not conform to protocol GMSAutoCompleteViewControllerDelegate in Swift
我收到错误:'ViewController' 不符合以下代码中的协议 'GMSAutoCompleteViewControllerDelegate'。
class MapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate {
...
@IBAction func autocompleteClicked(sender: AnyObject) {
let autoCompletController = GMSAutocompleteViewController()
autoCompletController.delegate = self
self.presentViewController(autoCompletController, animated: true, completion: nil)
}
//Handle user's selection
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithPlace place: GMSPlace!) {
//let placeName = place.name
//let placeAddress = place.formattedAddress
//let placeAttributions = place.attributions
let placeCoordinate = place.coordinate
mapView.camera = GMSCameraPosition(target: placeCoordinate, zoom: 15, bearing: 0, viewingAngle: 0)
}
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithError error: NSError!) {
// TODO: handle the error.
print("Error: ", error.description)
}
func wasCancelled(viewController: GMSAutocompleteViewController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
解决办法是什么?谢谢
您可能缺少 GMSMapViewDelegate
协议中的必需功能。 Ctrl+点击进入GMSMapViewDelegate
,查看列出的功能:没有列为@optional
的功能你都实现了吗?如果没有,swift 将无法编译。
您似乎输错了其中一项必需的功能。 didAutoCompleteWithError
func viewController(viewController: GMSAutocompleteViewController!, didFailAutocompleteWithError error: NSError!) {
// TODO: handle the error.
print("Error: ", error.description)
}
编辑:
GoogleDevelopers 文档中提供的有关 GMSAutocompleteViewControllerDelegate 协议参考的信息。
他们的文档中 google 提供的示例之一是 GMSAutocompleteViewControllerDelegate 的错误表示。该示例似乎是 Objective-C 等效项的纯翻译,因此对所需委托函数的描述有误。
比较example, with the actual docs here。记笔记
示例中的 didAutocompleteWithError
和
didFailAutocompleteWithError
在协议参考中。
如有错误请指正
我刚刚在 Swift 3 中收到此错误并将其更改为以下对我有用:
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Swift.Error) {
// TODO: handle the error.
print("Error: \(error.localizedDescription)")
}
我收到错误:'ViewController' 不符合以下代码中的协议 'GMSAutoCompleteViewControllerDelegate'。
class MapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate {
...
@IBAction func autocompleteClicked(sender: AnyObject) {
let autoCompletController = GMSAutocompleteViewController()
autoCompletController.delegate = self
self.presentViewController(autoCompletController, animated: true, completion: nil)
}
//Handle user's selection
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithPlace place: GMSPlace!) {
//let placeName = place.name
//let placeAddress = place.formattedAddress
//let placeAttributions = place.attributions
let placeCoordinate = place.coordinate
mapView.camera = GMSCameraPosition(target: placeCoordinate, zoom: 15, bearing: 0, viewingAngle: 0)
}
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithError error: NSError!) {
// TODO: handle the error.
print("Error: ", error.description)
}
func wasCancelled(viewController: GMSAutocompleteViewController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
解决办法是什么?谢谢
您可能缺少 GMSMapViewDelegate
协议中的必需功能。 Ctrl+点击进入GMSMapViewDelegate
,查看列出的功能:没有列为@optional
的功能你都实现了吗?如果没有,swift 将无法编译。
您似乎输错了其中一项必需的功能。 didAutoCompleteWithError
func viewController(viewController: GMSAutocompleteViewController!, didFailAutocompleteWithError error: NSError!) {
// TODO: handle the error.
print("Error: ", error.description)
}
编辑: GoogleDevelopers 文档中提供的有关 GMSAutocompleteViewControllerDelegate 协议参考的信息。
他们的文档中 google 提供的示例之一是 GMSAutocompleteViewControllerDelegate 的错误表示。该示例似乎是 Objective-C 等效项的纯翻译,因此对所需委托函数的描述有误。
比较example, with the actual docs here。记笔记
示例中的 didAutocompleteWithError
和
didFailAutocompleteWithError
在协议参考中。
如有错误请指正
我刚刚在 Swift 3 中收到此错误并将其更改为以下对我有用:
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Swift.Error) {
// TODO: handle the error.
print("Error: \(error.localizedDescription)")
}