经过大量研究,我想我已经发现了编码的问题。
我正在尝试设置一个应用程序,用户可以在到达后查看自己的位置。
我总是收到一个错误,上面写着
展开可选值时意外发现nil的致命错误(lldb)
错误显示为线程1:EXC_BAD_INSTRUCTION,位于以下行:
self.mapView.showsUserLocation = true
当我试图删除这一行时,另一个.mapView上会出现完全相同的错误。因此,错误出现在我使用mapkit和IBAOutlet(mapView)的每一行。
我似乎搞不清问题出在哪里。当我设置地图工具包时,我去了Main.Storyboard->CTR+drag->ViewController.swift,并称之为"mapView"
感谢您抽出时间
这是我的所有代码
import UIKit
import Parse
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
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)
}
}
这似乎是一个时间(或接口构建器连接)问题。
为了避免这种情况,请尝试将出口类型更改为MKMapView?
,并使用可选的coalesking,如:
self.mapView?.showsUserLocation = true
这至少解决了导致撞车的时间问题,这样你就可以更准确地确定问题所在。
如果崩溃后没有显示地图视图,这可能不是时间问题,而是接口生成器连接问题。请确保将地图视图连接到插座。
从var MapView中删除"weak"。如果您没有强烈的推荐信,该网点将立即取消分配。