2016-01-27 126 views
2

我得到的錯誤:'ViewController'不符合協議'GMSAutoCompleteViewControllerDelegate'在下面的代碼。ViewController不符合協議GMSAutoCompleteViewControllerDelegate在Swift

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

該修補程序是什麼?謝謝

+0

你讀過[文檔](https://developers.google.com/maps/documentation/ ios-sdk/reference/protocol_g_m_s_autocomplete_view_controller_delegate-p),並確保你已經實現了所需的功能? – Laffen

+0

是的,我從谷歌文檔得到這段代碼,並雙重檢查與我有所有必需的功能的API –

+1

可能重複[ios Swift - API GoogleMaps - 不符合協議GSMAutocompleteViewControllerDelegate](http://stackoverflow.com/questions/ 34676576/ios-swift-api-google-map-not-conform-protocol-gsmautocompleteviewcontrollerd) – Laffen

回答

2

似乎您錯誤地鍵入了所需功能之一。該didAutoCompleteWithError

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

編輯: 關於在Google代碼文檔中給出的GMSAutocompleteViewControllerDelegate協議的參考信息。

谷歌在他們的文檔中提供的一個例子是GMSAutocompleteViewControllerDelegate的錯誤表示。看來這個例子是Objective-C等價物的純粹翻譯,因此給出了所需委託函數的錯誤描述。

example與實際文檔here進行比較。請注意0​​與示例中的didAutocompleteWithError以及協議參考中的 didFailAutocompleteWithError

糾正我,如果我錯了。

+0

開發人員指南指出'didAutoCompleteWithError',但'didFailAutocompleteWithError'實際上是必要的調用 –

-1

你可能在GMSMapViewDelegate協議中缺少必需的功能。 Ctrl +點擊進入GMSMapViewDelegate並查看列出的功能:是否實現了所有未列爲@optional的功能?如果你還沒有,swift將不會編譯。

4

我只是在斯威夫特3收到此錯誤,並將其轉變爲我下面的工作:

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

大起來,救了我:) –

相關問題