1
私のアプリでオートコンプリート機能を実装しようとしています。Google APIオートコンプリート
ボタン付きのビューコントローラをセットアップし、以下のコードを追加しました。
しかし、私は、次のエラーを取得しておいてください。この行の
Cannot assign value of type 'selectAddress' to type 'GMSAutocompleteViewControllerDelegate?'
:これはなぜ
autocompleteController.delegate = self
誰でも知っていますか?
import Foundation
import UIKit
import GoogleMaps
import GooglePlaces
class selectAddress: UIViewController, UISearchBarDelegate {
// Present the Autocomplete view controller when the button is pressed.
@IBAction func autocompleteClicked(_ sender: UIButton) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
self.present(autocompleteController, animated: true, completion: nil)
}
}
extension ViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
func viewController(viewController: GMSAutocompleteViewController!, didFailAutocompleteWithError error: NSError!) {
// TODO: handle the error.
print("Error: ", error.description)
}
}
ありがとうございます – MattBlack