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
        }
    }

最佳答案

要填充圆圈(将UI控件连接到情节提要的@IBOutlet旁边),您需要同时打开.swift文件和情节提要,尝试执行以下操作以确保其实际连接


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


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

我还将列出有关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)
    }

}


那产生

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

关于ios - 我在Swift 10.2上运行自编码应用程序时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55464077/

10-13 09:27