我在Swift 10.2上运行自编码应用程序时遇到问题

时间:2019-04-01 21:50:41

标签: ios swift mapkit sigabrt

Xcode表示存在线程1:信号SIGABRT。它还说libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)。我是初学者,因此请“轻松”回复;)

//  ViewController.swift

import UIKit
import MapKit
import CoreLocation

class MapScreen: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        checkLocationServices()
    }

    func setupLocationManager() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }


    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuthorization()
        }else{
            // show alert letting the user know he has to turn them on.
        }
    }


    func checkLocationAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            mapView.showsUserLocation = true
            break
        case .denied:
            // show alert instructing how to turn on permissions
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .restricted:
            // show an alert letting them know what's up
            break
        case .authorizedAlways:
            break
        }
    }
}


    extension MapScreen: CLLocationManagerDelegate {
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            // later
            }
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            // later
        }
    }

1 个答案:

答案 0 :(得分:0)

要填充圆圈(将UI控件连接到情节提要的@IBOutlet旁边),您需要同时打开.swift文件和情节提要,并尝试确保它们确实已连接

  • 打开情节提要文件,然后打开MapScreen.swift文件,如果连接器已连接,则应填充连接器
  • 打开情节提要文件,然后单击以显示Assistant Editor,以便同时打开情节提要和MapScreen.swift文件,并确保在MapScreen中将Class设置为Identity Inspector,例如下面的屏幕截图

enter image description here

我还将列出一些有关MKMapView和LocationManager的建议


    如果locationManager.requestWhenInUseAuthorization()没有以下使用说明中的一项或多项,则
  • Info.plist将不起作用

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Message for AlwaysAndWhenInUseUsageDescription</string>
    
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Message for AlwaysUsageDescription</string>
    
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Message for WhenInUseUsageDescription</string>
    
  • 一旦获得使用位置服务的权限,就必须询问locationManager.startUpdatingLocation(),这样您才能通过CLLocationManagerDelegate

  • 获得位置更新
  • 位置更新是在locationManager(_:didUpdateLocations:)中接收的,为了能够在地图上查看用户位置,我们需要使用mapView.setRegion将地图区域设置为该位置

这是更新的课程

class MapScreen: UIViewController {

    @IBOutlet var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupLocationManager()
    }

    func setupLocationManager() {
        guard CLLocationManager.locationServicesEnabled() else {
            // show alert letting the user know he has to turn them on.
            print("Location Servies: Disabled")
            return
        }

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        checkLocationAuthorization()
    }

    func checkLocationAuthorization(authorizationStatus: CLAuthorizationStatus? = nil) {
        switch (authorizationStatus ?? CLLocationManager.authorizationStatus()) {
        case .authorizedAlways, .authorizedWhenInUse:
            locationManager.startUpdatingLocation()
            mapView.showsUserLocation = true
        case .restricted, .denied:
            // show alert instructing how to turn on permissions
            print("Location Servies: Denied / Restricted")
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        }
    }
}


extension MapScreen: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.count > 1 ? locations.sorted(by: { $0.timestamp < $1.timestamp }).last : locations.first else { return }

        let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let region = MKCoordinateRegion(center: location.coordinate, span: span)
        mapView.setRegion(region, animated: true)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        self.checkLocationAuthorization(authorizationStatus: status)
    }

}

产生

enter image description here