gpt4 book ai didi

ios - Swift : Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, 子代码=0x0) 发生错误

转载 作者:行者123 更新时间:2023-11-30 12:26:47 29 4
gpt4 key购买 nike

我正在制作一个使用 Google map /地点 API 的应用,并且正在使用 GMSAutocomplete View Controller 。一旦用户选择了一个位置,我就会在 map 和表格 View 上显示该位置的餐馆,但我在标题中收到上面提到的错误,我不知道为什么。

此指令发生错误:

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)
}
}

最佳答案

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

Swift code will terminate with this exception type if an unexpected condition is encountered at runtime such as:

a non-optional type with a nil List item value

a failed forced type conversion

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

关于ios - Swift : Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, 子代码=0x0) 发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44094354/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com