【问题标题】:Swift Xcode cant find MapKit as an outletSwift Xcode 找不到 MapKit 作为出口
【发布时间】: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


【解决方案1】:

这似乎是时间(或界面构建器连接)问题。

为避免这种情况,请尝试将插座的类型更改为 MKMapView? 并使用可选的合并,如下所示:

self.mapView?.showsUserLocation = true

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

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

【讨论】:

  • 欧凯!这个作品!我不确定它是否会显示我的位置!但它确实显示了苹果!但是,这是什么意思?怎么了?
  • 你是否使用过模拟器?;)
  • 是的,先生!再次,谢谢你。不敢相信,经过 2 天的搜索并试图弄清楚,我只需要一个问号......
【解决方案2】:

从 var MapView 中删除“弱”。如果您没有强参考,则插座将立即取消分配。

【讨论】:

    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2021-09-17
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多