2016-11-12 77 views
1

我最近將我的應用程序升級到了swift 3,並且每當它試圖在谷歌地圖中調用自動完成視圖控制器時,它都會崩潰。我知道一個API密鑰不是問題,因爲它在我升級到swift 3和xcode 8之前使用它時正常工作。我也知道這是因爲該應用程序仍然能夠顯示谷歌地圖。 這裏是我的代碼:GMSAutoCompleteViewController崩潰 - Swift 3

extension AdditionalSetupViewController: GMSAutocompleteViewControllerDelegate { 

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { 
     let locationCoordinate = place.coordinate 

     if homeClicked { 
      homeAddress = [locationCoordinate.latitude, locationCoordinate.longitude] 
     } 

     else if homeAddress == [] { 
     } 

     else { 

      let coordString = "\(locationCoordinate.latitude) \(locationCoordinate.longitude)".replacingOccurrences(of: ".", with: ",") 

//   let coordString = "\(locationCoordinate.latitude) \(locationCoordinate.longitude)".stringByReplacingOccurrencesOfString(".", withString: ",") 

      ref = FIRDatabase.database().reference() 

      let currentUser: String = (FIRAuth.auth()?.currentUser?.uid)! 
      let address = ref.child("schoolOrWorkAddress").child(coordString) 
      let childUpdates = [currentUser: homeAddress] 
      address.updateChildValues(childUpdates) 

      finishButton.alpha = 1 
      finishButton.isEnabled = true 
     } 

     let schoolorWorkCoords = "\(locationCoordinate.latitude) \(locationCoordinate.longitude)".replacingOccurrences(of: ".", with: ",") 

//  let schoolorWorkCoords = "\(locationCoordinate.latitude) \(locationCoordinate.longitude)".stringByReplacingOccurrencesOfString(".", withString: ",") 
     let userID = (FIRAuth.auth()?.currentUser?.uid)! 
     _ = ref.child("posts").child(userID).key 
     let additionalBasicInfo = ["schoolOrWorkAddress": schoolorWorkCoords, "homeAddress": homeAddress] as [String : Any] 
     let usersRef = ref.child("users/\(userID)") 
     usersRef.updateChildValues(additionalBasicInfo as [NSObject : AnyObject]) 

     self.dismiss(animated: true, completion: nil) 

    } 


    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { 
     print(error) 
    } 

    func wasCancelled(_ viewController: GMSAutocompleteViewController) { 
     self.dismiss(animated: true, completion: nil) 
    } 

    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { 
     UIApplication.shared.isNetworkActivityIndicatorVisible = true 
    } 

    func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { 
     UIApplication.shared.isNetworkActivityIndicatorVisible = false 
    } 

} 

以下是錯誤輸出:

[Pars2.AdditionalSetupViewController addYourSchoolOrWorkAddress:]: unrecognized selector sent to instance 0x7fc4c8d7f200 
2016-11-12 13:55:31.755 Pars2[3065:191093] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Pars2.AdditionalSetupViewController addYourSchoolOrWorkAddress:]: unrecognized selector sent to instance 0x7fc4c8d7f200' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001068f534b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x000000010635621e objc_exception_throw + 48 
    2 CoreFoundation      0x0000000106964f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 
    3 CoreFoundation      0x000000010687ac15 ___forwarding___ + 1013 
    4 CoreFoundation      0x000000010687a798 _CF_forwarding_prep_0 + 120 
    5 UIKit        0x0000000104b095b8 -[UIApplication sendAction:to:from:forEvent:] + 83 
    6 UIKit        0x0000000104c8eedd -[UIControl sendAction:to:forEvent:] + 67 
    7 UIKit        0x0000000104c8f1f6 -[UIControl _sendActionsForEvents:withEvent:] + 444 
    8 UIKit        0x0000000104c8e0f2 -[UIControl touchesEnded:withEvent:] + 668 
    9 UIKit        0x0000000104b76ce1 -[UIWindow _sendTouchesForEvent:] + 2747 
    10 UIKit        0x0000000104b783cf -[UIWindow sendEvent:] + 4011 
    11 UIKit        0x0000000104b2563f -[UIApplication sendEvent:] + 371 
    12 UIKit        0x000000010531771d __dispatchPreprocessedEventFromEventQueue + 3248 
    13 UIKit        0x00000001053103c7 __handleEventQueue + 4879 
    14 CoreFoundation      0x000000010689a311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    15 CoreFoundation      0x000000010687f59c __CFRunLoopDoSources0 + 556 
    16 CoreFoundation      0x000000010687ea86 __CFRunLoopRun + 918 
    17 CoreFoundation      0x000000010687e494 CFRunLoopRunSpecific + 420 
    18 GraphicsServices     0x0000000108f02a6f GSEventRunModal + 161 
    19 UIKit        0x0000000104b07964 UIApplicationMain + 159 
    20 Pars2        0x00000001010a078f main + 111 
    21 libdyld.dylib      0x00000001078d468d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

我不就好像這能幫助,但這裏是我的視圖控制器的其餘部分的代碼,以及:

class AdditionalSetupViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 


    override func viewDidLoad() { 
     finishButton.alpha = 0.4 
     finishButton.isEnabled = false 

     navigationController?.navigationBar.barTintColor = UIColor(red: 46, green: 204, blue: 113, alpha: 0) 
    } 


    @IBOutlet weak var mapView : GMSMapView! 
    var homeClicked = false 
    var homeAddress: [Double] = [] 

    @IBAction func addYourHomeAddress(sender: AnyObject) { 
     homeClicked = true 
     createGMSViewController() 
    } 

    @IBAction func addYourSchoolOrWorkAddress(sender: AnyObject) { 
     homeClicked = false 
     createGMSViewController() 
    } 

    func createGMSViewController() { 
     let searchAutoCompleteController = GMSAutocompleteViewController() 
     searchAutoCompleteController.delegate = self 
     self.present(searchAutoCompleteController, animated: true, completion: nil) 
    } 

    @IBOutlet weak var finishButton: UIButton! 

    @IBAction func finishButton(sender: AnyObject) { 

     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 

     let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TableView") as! UITabBarController 
     self.present(nextViewController, animated:true, completion:nil) 

    } 


} 

我不知道爲什麼它崩潰時,它會打電話給我,但任何幫助將不勝感激!

回答

1

好吧,事實證明,GMS自動完成視圖控制器工作正常,而不是它是一個插座問題。解決方法是簡單地刪除當前的xcode 7或之前版本的插座,然後創建一個新的插座,因爲xcode 8中的插座略有不同。