我最近将我的应用程序升级到了swift 3,并且只要它试图在谷歌地图中调用自动完成视图控制器,它就会崩溃。我知道api键不是问题因为它在我升级到swift 3和xcode 8之前使用它时工作正常。我也知道这个因为应用程序仍然能够显示谷歌地图。 这是我的代码:
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)
}
}
我不知道为什么它会在它调用时崩溃,但任何帮助都会受到赞赏!
答案 0 :(得分:2)
好的,事实证明,GMS自动完成视图控制器工作正常,而不是出口问题。解决方案是删除当前的xcode 7或以前版本的插件,然后创建一个新的插件,因为xcode 8中的插座略有不同。