【发布时间】:2016-01-12 15:25:45
【问题描述】:
经过大量研究,我认为我发现了我的编码问题。
我正在尝试设置一个应用程序,用户可以在其中看到他们所在的位置。
我总是收到一个错误提示
在展开可选值时意外发现 nil 致命错误 (lldb)
并且错误显示为 Thread 1: EXC_BAD_INSTRUCTION 在这一行:
self.mapView.showsUserLocation = true
当我尝试删除该行时,另一个 .mapView 上会显示完全相同的错误。所以错误出现在我使用我的 mapkit 和 IBAOutlet(mapView) 的每一行。
我似乎无法弄清楚问题所在。当我为 mapkit 设置属性时,我去了 Main.Storyboard -> CTR + 拖动 -> 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)
}
}
【问题讨论】:
-
您是否尝试过移除情节提要中的插座连接并重新连接?
标签: ios swift mapkit core-location