我是stackexchange的新手,所以如果我需要提供更多信息,请告诉我。
我尝试安装Corelocation和Mapkit并使用它们来实现。自从我跟随Vea软件教程后,我发现了以下错误:
主题1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)
在编译器中它说
致命错误:在打开Optional时意外发现nil 值(LLDB)
错误正好在这一行,但当我删除它时,它出现在另一个Mapkit / Location核心行。错误:
self.mapView.showsUserLocation = true
编辑:当我删除上面的行时,
上会出现相同的错误 self.mapView.setRegion(region, animated: true)
我一直在互联网上搜索一段时间,但没有什么能真正帮助我。谢谢你的时间。
如果您需要整个viewcontroller代码。在这里。
import UIKit
import Parse
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTF: UITextField!
@IBOutlet var EmailTF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Location Delegate Methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error:" + error.localizedDescription)
}
}
答案 0 :(得分:1)
这只是一个假设,我注意到你使用
self.mapView.setRegion(region,animated:true)
在CLLocationManager委托中,并在mapView实际显示之前开始更新(即在viewDidLoad中)。
可能是mapView在安装到视图层次结构之前尝试更新其可视状态吗?