Ios myLocationEnabled-在展开时意外发现零
我只是尝试使用GoogleMaps API获取用户在我的MapViewController中的位置。 我环顾了所有地方,不明白为什么我不断地遇到致命错误:在这个特定的地方展开时意外地发现了nil和可选值 这是我的密码:Ios myLocationEnabled-在展开时意外发现零,ios,swift,mkmapview,cllocationmanager,Ios,Swift,Mkmapview,Cllocationmanager,我只是尝试使用GoogleMaps API获取用户在我的MapViewController中的位置。 我环顾了所有地方,不明白为什么我不断地遇到致命错误:在这个特定的地方展开时意外地发现了nil和可选值 这是我的密码: class MapViewController: UIViewController { let locationManager = CLLocationManager() @IBOutlet weak var mapView: GMSMapView!
class MapViewController: UIViewController {
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera = GMSCameraPosition.cameraWithLatitude(-34.9290, longitude:138.6010, zoom:10)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
mapView.mapType = kGMSTypeNormal
print("Calling addMarker")
// addMarker()
self.view = mapView
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
这就是我得到错误的地方:
!!()
我知道
mapView.myLocationEnabled
返回为零,但不理解为什么/如何返回。很可能是mapView为零,而不是mapView.myLocationEnabled。当发生崩溃时,请尝试以下lldb命令:
po mapView
这将确切地告诉你
didChangeAuthorizationStatus委托可能正在后台调用,或者在视图完成之前调用。您应该将这些扩展移到AppDelegate中。谢谢您的回复。事实上,您是对的,po mapView显示mapView是一个未解析的标识符。但是,将扩展移到AppDelegate中并没有修复错误:(IBOutlet是否连接到Interface Builder中的相应视图?