Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIFT线程1:EXC错误指令XCODE IOS_Ios_Xcode_Swift_Mapkit_Core Location - Fatal编程技术网

SWIFT线程1:EXC错误指令XCODE IOS

SWIFT线程1:EXC错误指令XCODE IOS,ios,xcode,swift,mapkit,core-location,Ios,Xcode,Swift,Mapkit,Core Location,我是stackexchange的新手,如果需要提供更多信息,请告诉我 我尝试安装Corelocation和Mapkit,并将它们一起使用。自从我学习Vea软件教程以来,每次都会遇到以下错误: 线程1:EXC_BAD_指令(代码=EXC_1386_INVOP,子代码=0x0) 在编译器中,它说 致命错误:在展开可选文件时意外发现nil 值(llDB) 错误正好出现在这一行,但当我删除它时,它会出现在另一个Mapkit/Location核心行。错误: self.mapView.showsUserLo

我是stackexchange的新手,如果需要提供更多信息,请告诉我

我尝试安装Corelocation和Mapkit,并将它们一起使用。自从我学习Vea软件教程以来,每次都会遇到以下错误:

线程1:EXC_BAD_指令(代码=EXC_1386_INVOP,子代码=0x0)

在编译器中,它说

致命错误:在展开可选文件时意外发现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)
    }

}

这只是我注意到你使用的一个假设

self.mapView.setRegion(区域,动画:true)

在实际显示mapView之前(即在viewDidLoad中),委托并开始更新


是否mapView在安装到视图层次结构中之前正在尝试更新其视觉状态

所有带有感叹号的变量都将被强制展开,因此这些变量中的任何一个都可能因该错误而导致应用程序崩溃。同样在你的代码中,我看到了
让location=locations。最后
然后你使用
location。有没有可能位置为零?我尝试进入调试->模拟位置,选择一个随机位置,然后尝试运行它。仍然会遇到同样的错误,但这并不能真正回答我的问题。我将把它视为“否”。错误消息告诉我们的是,这些选项中的任何一个
@IBOutlet-weak-var-mapView:MKMapView@IBOutlet var UsernameTextField:UITextField@IBOUTLE var密码tf:UITextField@IBOutlet var EmailTF:UITextField!让location=locations.last
为.None(nil)而不是being。也许如果你的xib他们引用的东西不再存在,我很抱歉。我认为我已经从脚本中删除了这些内容,以保持其相关性。在我尝试添加Mapkit和Corelocation之前,这些代码运行得很好。而且我刚刚注意到,对于大多数出口,您的变量名都是大写的。不知道你是不是有意的。对不起,我不太明白你的意思。在更新位置之前,我尝试移动self.mapView.setRegion。不起作用。当我删除self.mapView.showsUserlocation(错误所在的位置)时,错误会出现在另一个.mapView上。(self.mapView.setRegion)我无法测试这一点,但根据使用其他组件的类似经验,我建议您将self.locationManager.startUpdatingLocation()和self.mapView.showsUserLocation=true移动到视图层次结构就绪后执行的函数。e、 g.viewwillbeep或viewdidapper当我将“self.mapView.showsUserLocation=true”放在“func locationManager(manager:CLLocationManager,didUpdateLocations:[CLLocation]){”下时,应用程序运行正常,没有错误,但它不会查找我的位置。你尝试了我的建议吗?例如,移动self.locationManager.startUpdatingLocation()和self.mapView.showsUserLocation=true to viewDidDisplay()