【问题标题】:ViewController does not conform to protocol GMSAutoCompleteViewControllerDelegate in SwiftViewController 不符合 Swift 中的协议 GMSAutoCompleteViewControllerDelegate
【发布时间】:2016-05-05 01:25:23
【问题描述】:

我收到错误消息:“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)
    }
}

解决办法是什么?谢谢

【问题讨论】:

标签: ios swift google-maps autocomplete google-places-api


【解决方案1】:

您可能缺少GMSMapViewDelegate 协议中的必需功能。 Ctrl+点击进入GMSMapViewDelegate,查看列出的功能:你实现了@optional以外的所有功能吗?如果没有,swift 将无法编译。

【讨论】:

    【解决方案2】:

    您好像打错了其中一项必需的功能。 didAutoCompleteWithError

    func viewController(viewController: GMSAutocompleteViewController!, didFailAutocompleteWithError error: NSError!) {
        // TODO: handle the error.
        print("Error: ", error.description)
    }
    

    编辑: GoogleDevelopers 文档中提供的有关 GMSAutocompleteViewControllerDelegate 协议参考的信息。

    Google 在其文档中提供的示例之一是 GMSAutocompleteViewControllerDelegate 的错误表示。该示例似乎是对 Objective-C 等价物的纯翻译,因此对所需委托函数的描述是错误的。

    example 与实际文档here 进行比较。记笔记 到示例中的didAutocompleteWithError 和 协议参考中的didFailAutocompleteWithError

    如果我错了,请纠正我。

    【讨论】:

    • 开发者指南指出 'didAutoCompleteWithError' 但是 'didFailAutocompleteWithError' 实际上是必要的调用
    【解决方案3】:

    我刚刚在 Swift 3 中收到此错误,并将其更改为以下对我有用:

    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Swift.Error) {
        // TODO: handle the error.
        print("Error: \(error.localizedDescription)")
    }
    

    【讨论】:

    • 大,救了我:)
    猜你喜欢
    • 1970-01-01
    • 2016-11-17
    • 2014-07-28
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多