gpt4 book ai didi

ios - Swift Xcode 找不到 MapKit 作为导出

转载 作者:行者123 更新时间:2023-11-28 06:51:33 31 4
gpt4 key购买 nike

经过大量研究,我认为我发现了我的编码问题。

我正在尝试设置一个应用程序,用户可以在其中查看自己的位置。

我总是收到一个错误提示

fatal error unexpectedly found nil while unwrapping an optional value (lldb)

错误显示为 Thread 1: EXC_BAD_INSTRUCTION 在此行:

self.mapView.showsUserLocation = true

当我尝试删除该行时,完全相同的错误出现在另一个 .mapView 上。所以错误出现在我使用我的 mapkit 和 IBAOutlet(mapView) 的每一行。

我似乎无法弄清楚问题出在哪里。当我对 mapkit 进行属性化时,我去了 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? 并像这样使用可选的合并:

self.mapView?.showsUserLocation = true

这至少解决了导致崩溃的时序问题,因此您可以更准确地查明问题。

如果崩溃后没有显示 map View ,则可能不是时间问题,而是界面构建器连接问题。然后确保将 map View 连接到 socket 。

关于ios - Swift Xcode 找不到 MapKit 作为导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34747502/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com