Reputation: 3828
I am trying to implement an autocomplete feature within my app.
I have set up a view controller with a button and added the code below.
However I keep getting the following error:
Cannot assign value of type 'selectAddress' to type 'GMSAutocompleteViewControllerDelegate?'
on this line:
autocompleteController.delegate = self
Does anyone know why this is?
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)
}
}
Upvotes: 2
Views: 206
Reputation: 5523
You are setting the delegate to self
, which is a class of type selectAddress
, but the extension you have that conforms to the GMSAutocompleteViewControllerDelegate
protocol is of type ViewController
. If you change that to extension selectAddress: GMSAutocompleteViewControllerDelegate
you'll be set.
Upvotes: 3