【发布时间】:2016-01-11 20:09:50
【问题描述】:
我是 stackexchange 的新手,所以如果我需要提供更多信息,请告诉我。
我尝试安装 Corelocation 和 Mapkit 并一起使用它们。自从我遵循 Vea Software Tutorial 以来,我都收到以下错误:
线程 1:EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)
在编译器中它说
致命错误:在展开 Optional 时意外发现 nil 值(llDB)
错误出现在这一行,但是当我删除它时,它出现在另一个 Mapkit/Location 核心行。错误:
self.mapView.showsUserLocation = true
编辑:当我删除上面的行时,同样的错误出现在
self.mapView.setRegion(region, animated: true)
我在互联网上搜索了一段时间,但没有什么能真正帮助我。感谢您的宝贵时间。
如果您需要完整的视图控制器代码。在这里。
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)
}
}
【问题讨论】:
-
所有带有感叹号的变量都被强制解包,因此其中任何一个都可能导致您的应用程序因该错误而崩溃。同样在您的代码中,我看到
let location = locations.last,然后您使用location!。任何机会位置都为零? -
我尝试进入调试 -> 模拟位置,选择了一个随机位置,然后尝试运行它。仍然遇到同样的错误
-
这并不能真正回答我的问题,我会认为它是否定的。什么错误消息告诉的是,这些选项中的任何一个
@IBOutlet weak var mapView: MKMapView! @IBOutlet var UsernameTextField: UITextField! @IBOutlet var PasswordTF: UITextField! @IBOutlet var EmailTF: UITextField! let location = locations.last是 .None (nil) 而不是 .Some 所以我的建议是检查这些。也许如果您的 xib 他们正在引用不再存在的东西 -
对不起。我以为我已经从脚本中删除了这些以保持相关性。在我尝试添加 Mapkit 和 Corelocation 之前,这些代码运行良好。
-
另外我刚刚注意到您的变量名称对于大多数出口都是大写的。不知道你是不是故意的。
标签: ios xcode swift mapkit core-location