Google Maps/Places APIを使用するアプリケーションを作成していますが、GMSAutocomplete View Controllerを使用しています。ユーザーが場所を選択すると、その場所のレストランが地図とテーブルビューの両方に表示されますが、タイトルに上記のエラーが表示され、その理由がわかりません。Swfit:スレッド1:EXC_BAD_INSTRUCTION(コード= EXC_1386_INVOP、サブコード= 0x0)エラーが発生しました
エラーは、この命令で行わ:場合
mapViewController.mapView.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)
}
}
感謝を。私はあなたが提案したことを試しましたが、問題は依然として続きます。私はより便利に上記のコードを更新しました。それ以上の提案がある場合は、躊躇しないで共有してください。 – notSoExperiencedCoder
その場合、 'mapView'はおそらくインスタンス化されていません。 – Oskar
あなたはmapViewが適切にインスタンス化されていなかったので、最初にアプリケーションを開いてlistViewControllerからAutocomplete View Controllerを開くと、mapViewControllerには行かないことがわかりました。アプリケーションはクラッシュしますそれはmapViewControllerも見ていないからです。だから、今私の質問は、あなたが私がそれをやって行くことをどのように提案するかです。これがかなり基本的な質問のように思えるなら、私はお詫び申し上げます。私はまだSwiftには少し新しく、そのすべてはニュアンスです。 – notSoExperiencedCoder