CLLocationManager在模拟器上继续生成EXC_BAD_INSTRUCTION

时间:2015-09-10 03:43:34

标签: ios xcode swift cllocationmanager

在我的视图控制器中,有MKMapView以用户当前位置为中心。

class MyViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

let locationManager = CLLocationManager()
let map : MKMapView! = MKMapView()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    map.delegate = self
    map.showsUserLocation = true

    var region = MKCoordinateRegion()
    region.center.latitude = (locationManager.location?.coordinate.latitude)!
    region.center.longitude = (locationManager.location?.coordinate.longitude)!
    region.span = MKCoordinateSpanMake(0.2, 0.2)
    map.setRegion(region, animated: true)       
}

问题是,每当我在iOS模拟器上运行此程序时,它都会在此行崩溃:

region.center.latitude = (locationManager.location?.coordinate.latitude)!

并生成EXC_BAD_INSTRUCTION错误。但是,当我在iPhone上运行它时,它工作得很好。我已将部署目标设置为iOS 8.0。有谁知道为什么?

1 个答案:

答案 0 :(得分:0)

不知何故,我设法通过简单地将region.center设置代码更改为

来解决问题
region.center.latitude = locationManager.location!.coordinate.latitude
region.center.longitude = locationManager.location!.coordinate.longitude