gpt4 book ai didi

swift - 谷歌 API 自动完成

转载 作者:可可西里 更新时间:2023-11-01 00:57:24 25 4
gpt4 key购买 nike

我正在尝试在我的应用中实现自动完成功能。

我已经设置了一个带有按钮的 View Controller 并添加了下面的代码。

但是我不断收到以下错误:

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

}

最佳答案

您正在将委托(delegate)设置为 self,这是一个类型为 selectAddress 的类,但是您拥有的扩展符合 GMSAutocompleteViewControllerDelegate协议(protocol)是 ViewController 类型。如果您将其更改为 extension selectAddress: GMSAutocompleteViewControllerDelegate,您将被设置。

关于swift - 谷歌 API 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42845542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com