2017-10-13 167 views
2

任何人都可以在使用Google Places時幫助如何解決Swift3中的這個警告嗎?Google Places API Warning

「API API警告:在使用前沒有在GMSAutocompleteResultsViewController實例上設置代理請注意,這可能導致未定義的行爲,例如無法關閉屏幕,未被通知選擇,以及無法正確管理對象生命週期「。

我基本上遵循https://developers.google.com/places/ios-api/autocomplete教程[搜索欄添加到視圖頂部]

代碼:

import UIKit[enter image description here][1] 
import GooglePlaces 

class LocationTabViewController: UIViewController {   
     var resultsViewController: GMSAutocompleteResultsViewController? 
     var searchController: UISearchController? 
     var resultView: UITextView? 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      resultsViewController = GMSAutocompleteResultsViewController() 

      resultsViewController?.delegate = self as? GMSAutocompleteResultsViewControllerDelegate 

      searchController = UISearchController(searchResultsController: resultsViewController) 

      searchController?.searchResultsUpdater = resultsViewController 

      let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0)) 

      subView.addSubview((searchController?.searchBar)!) 

      view.addSubview(subView) 

      searchController?.searchBar.sizeToFit() 

      searchController?.hidesNavigationBarDuringPresentation = false 

      // When UISearchController presents the results view, present it in 

      // this view controller, not one further up the chain. 

      definesPresentationContext = true 

     } 

    } 



    // Handle the user's selection. 

    extension ViewController: GMSAutocompleteResultsViewControllerDelegate { 

    func resultsController(_ resultsController: GMSAutocompleteResultsViewController, 

           didAutocompleteWith place: GMSPlace) { 

      // Do something with the selected place. 

      print("Place name: \(place.name)") 

      print("Place address: \(String(describing: place.formattedAddress))") 

      print("Place attributions: \(String(describing: place.attributions))") 

     } 



     func resultsController(_ resultsController: GMSAutocompleteResultsViewController, 

           didFailAutocompleteWithError error: Error){ 

      // TODO: handle the error. 

      print("Error: ", error.localizedDescription) 

     } 



     // Turn the network activity indicator on and off again. 

     func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { 

      UIApplication.shared.isNetworkActivityIndicatorVisible = true 

     } 



     func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { 

      UIApplication.shared.isNetworkActivityIndicatorVisible = false 

     } 

} 

回答

1

你只需要委託設置到控制器,就像這樣:

resultsViewController = GMSAutocompleteResultsViewController() 
resultsViewController?.delegate = self 

這應該擺脫警告。

祝你好運!

+0

有一個錯誤說:不能指定類型''LocationTabViewController'的值來鍵入「GMSAutocompleteResultsViewControllerDelegate?」並修復它說:「插入爲! GMSAutocompleteResultsViewControllerDelegate「。 – Rocky

+0

@Rocky我相信你必須在這種情況下使用bang運算符'resultsViewController?.delegate = self as!GMSAutocompleteResultsViewControllerDelegate',這樣你就不會返回一個可選的。更多關於類型casting [here](https:// www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwiSt97bzvTWAhVLwFQKHS_aDRIQFgg6MAM&url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fcontent%2Fdocumentation%2FSwift%2FConceptual% 2FSwift_Programming_Language%2FTypeCasting.html&USG = AOvVaw2PrYfF_q4lKcQlBeymrY1K) –