Swfit:线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)错误发生

时间:2017-05-21 07:38:51

标签: ios swift google-maps google-places-api

我正在创建一个使用Google Maps / Places API的应用,并且我正在使用GMSAutocomplete View Controller。一旦用户选择了一个位置,我就会在地图和桌面视图上显示该位置的餐馆,但我在标题中收到上述错误,我不确定原因。

此指令发生错误:

  

mapViewController.mapView.camera = camera

// OUTLETS
@IBOutlet weak var mapButton: UIButton!
@IBOutlet weak var listButton: UIButton!
@IBOutlet weak var container: UIView!
@IBOutlet var toggle : [UIButton]!

// VARIABLES

var listViewController = exploreListViewController!
var mapViewController = exploreMapsViewController!

var viewControllers : [UIViewController]!
var selectedIndex : Int = 0

var listVC = exploreListViewController()

override func viewDidLoad() {
    super.viewDidLoad()


    listButton.roundCorners([.topLeft,.bottomLeft], radius: 5, borderColor: nil, borderWidth: nil)
    mapButton.roundCorners([.topRight,.bottomRight], radius: 5, borderColor: UIColor.white, borderWidth: 5)


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

    listViewController = storyBoard.instantiateViewController(withIdentifier: "list") as! exploreListViewController
    mapViewController = storyBoard.instantiateViewController(withIdentifier: "maps") as! exploreMapsViewController

    viewControllers = [listViewController, mapViewController]
    toggle[selectedIndex].isSelected = true
    toggleViews(toggle[selectedIndex])

}

@IBAction func toggleViews(_ sender: UIButton) {

    // REMOVING PREVIOUS VIEW CONTROLLER

    let previousIndex = selectedIndex
    let previousVC = viewControllers[previousIndex]

    selectedIndex = sender.tag
    toggle[previousIndex].isSelected = false

    previousVC.willMove(toParentViewController: nil)
    previousVC.view.removeFromSuperview()
    previousVC.removeFromParentViewController()

    // ADDING SELECTED VIEW CONTROLLER

    sender.isSelected = true
    let currentVC = viewControllers[selectedIndex]

    addChildViewController(currentVC)
    currentVC.view.frame = container.bounds
    container.addSubview(currentVC.view)
    currentVC.didMove(toParentViewController: self)
}

@IBAction func switchToList(_ sender: UIButton) {

    listButton.setTitleColor(UIColor.red, for: .normal)
    listButton.backgroundColor = UIColor.white
    listButton.roundCorners([.bottomLeft,.topLeft], radius: 5, borderColor: nil, borderWidth: nil)

    mapButton.setTitleColor(UIColor.white, for: .normal)
    mapButton.backgroundColor = UIColor.red
    mapButton.roundCorners([.bottomRight,.topRight], radius: 5, borderColor: UIColor.white, borderWidth: 5)

}

@IBAction func switchToMap(_ sender: UIButton) {

    mapButton.setTitleColor(UIColor.red, for: .normal)
    mapButton.backgroundColor = UIColor.white
    mapButton.roundCorners([.bottomRight,.topRight], radius: 5, borderColor: nil, borderWidth: nil)

    listButton.setTitleColor(UIColor.white, for: .normal)
    listButton.backgroundColor = UIColor.red
    listButton.roundCorners([.bottomLeft,.topLeft], radius: 5, borderColor: UIColor.white, borderWidth: 5)

}

// MARK: GOOGLE AUTOCOMPLETE DELEGATE


// WHEN USER SUCCESSFULLY CHOOSES LOCATION

public func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace){

    let googleURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(place.coordinate.latitude),\(place.coordinate.longitude)&types=restaurants&radius=50000&key=MY_API_KEY"

    let cityPicture = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(place.coordinate.latitude),\(place.coordinate.longitude)&types=locality&radius=50000&key=MY_API_KEY"

    let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15)

    // LIST VIEW

    listViewController.cityName.removeAll()
    listViewController.cityURL.removeAll()
    listViewController.placeNames.removeAll()
    listViewController.imageURL.removeAll()

    listViewController.cityLabel.text = String(describing: place.name)
    listViewController.getCityInfo(url: cityPicture)
    listViewController.callAlamo(url: googleURL)

    // MAP VIEW

    mapViewController.mapView.camera = camera // THIS IS WHERE THE ERROR OCCURS
    mapViewController.callAlamo(url: googleURL)
    self.dismiss(animated: true, completion: nil)
}

// WHEN AN ERROR OCCURS

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

    print("ERROR: ",error)
}

// WHEN USER PRESSES CANCEL BUTTON

public func wasCancelled(_ viewController: GMSAutocompleteViewController){

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

@IBAction func openPlacePicker(_ sender: UIButton) {

    let autoCompleteController = GMSAutocompleteViewController()

    autoCompleteController.delegate = self
    self.present(autoCompleteController, animated: true, completion: nil)
    }
 }

2 个答案:

答案 0 :(得分:0)

https://developer.apple.com/library/content/technotes/tn2151/_index.html

  

如果在运行时遇到意外情况,则Swift代码将以此异常类型终止,例如:

     

具有零列表项值

的非可选类型      

失败的强制类型转换

所以我可以想象你尝试使用零数据。检查mapViewController是否有值。

答案 1 :(得分:0)

您的tempVC已分配给mapViewController,但从未初始化。

拥有tempXX vars的原因是什么?这会让您的代码非常混乱。

无论如何,请更改:

var mapViewController : exploreMapsViewController!

要:

var mapViewController = exploreMapsViewController()
相关问题