CLLocationManager 在模拟器上不断生成 EXC_BAD_INSTRUCTION
CLLocationManager keeps generating EXC_BAD_INSTRUCTION on simulators
在我的视图控制器中有一个 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。有谁知道为什么?
我认为在您的模拟器中,locationManager 实例中没有位置对象。我建议您检查可选值,看看该值是否不为零。
NSLog("\(locationManager.location?)")
我设法通过简单地将 region.center 设置代码更改为
来解决问题
region.center.latitude = locationManager.location!.coordinate.latitude
region.center.longitude = locationManager.location!.coordinate.longitude
在我的视图控制器中有一个 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。有谁知道为什么?
我认为在您的模拟器中,locationManager 实例中没有位置对象。我建议您检查可选值,看看该值是否不为零。
NSLog("\(locationManager.location?)")
我设法通过简单地将 region.center 设置代码更改为
来解决问题region.center.latitude = locationManager.location!.coordinate.latitude
region.center.longitude = locationManager.location!.coordinate.longitude