【发布时间】:2016-08-31 04:32:54
【问题描述】:
我正在尝试构建一个应用程序,我在其中将文本输入到 textField 并获得谷歌通过其 Fetcher GooglePlaces API 提供的自动预测。我已经复制了代码并为我的应用程序进行了更改,但我无法弄清楚我做错了什么。我正在使用 Swift Xcode8 Beta 3。
请看下面我的代码。
import UIKit
import GooglePlaces
import GoogleMaps
class NewTripAddCitiesViewController: UIViewController {
var fetcher: GMSAutocompleteFetcher?
@IBOutlet var textView: UITextView!
@IBOutlet var placesTextField: UITextField!
@IBOutlet var addToTripLabel: UIButton!
@IBOutlet var addCitiesLabel: UILabel!
@IBAction func addPlaceToTable(_ sender: AnyObject) {
}
@IBAction func savePlaces(_ sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
addCitiesLabel.text = "Please select which places you'd like to visit in \(tripCountry):"
addToTripLabel.setTitle("Add to \"\(tripName)\"", for: [])
self.view.backgroundColor = UIColor.white()
self.edgesForExtendedLayout = []
// Set bounds to inner-west Sydney Australia.
let neBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366, longitude: 151.134002)
let swBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725, longitude: 151.200349)
let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner, coordinate: swBoundsCorner)
// Set up the autocomplete filter.
let filter = GMSAutocompleteFilter()
filter.type = .establishment
// Create the fetcher.
fetcher = GMSAutocompleteFetcher(bounds: bounds, filter: filter)
fetcher!.delegate = self
placesTextField = UITextField(frame: CGRect(x: 5.0, y: 0, width: self.view.bounds.size.width - 5.0, height: 44.0))
placesTextField?.autoresizingMask = .flexibleWidth
placesTextField?.addTarget(self, action: Selector(("textFieldDidChange:")), for: .editingChanged)
textView = UITextView(frame: CGRect(x: 0, y: 45.0, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 45.0))
textView?.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
textView?.text = "No Results"
textView?.isEditable = false
self.view.addSubview(placesTextField!)
self.view.addSubview(textView!)
}
func textFieldDidChange(placesTextField: UITextField) {
fetcher?.sourceTextHasChanged(placesTextField.text!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension NewTripAddCitiesViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
let resultsStr = NSMutableAttributedString()
for prediction in predictions {
resultsStr.append(prediction.attributedPrimaryText)
resultsStr.append(AttributedString(string: "\n"))
}
textView?.attributedText = resultsStr
}
func didFailAutocompleteWithError(_ error: NSError) {
textView?.text = error.localizedDescription
}
}
【问题讨论】:
-
PS 我在这里收到错误:extension NewTripAddCitiesViewController: GMSAutocompleteFetcherDelegate {
-
好吧,继续实现编译器抱怨的协议方法。您应该在问题导航器中看到它们。
-
当你遇到这个问题时,只需在委托上按 CMD+CLIK,它会带你到它的文档,在那里你会看到一堆函数,只需添加这些
-
我这样做了...该协议需要 didAutocompleteWithPredictions 方法和 didFailAutocompleteWithError 方法,我在代码中都使用了这两种方法...可能与桥接头有关吗?我对此很陌生。我什至制作了一个新的视图控制器,并直接从谷歌开发人员那里逐字复制了代码,但仍然得到同样的错误......真令人沮丧
标签: swift google-places-api swift3 xcode8-beta3