gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:53:12 25 4
gpt4 key购买 nike

Xcode 说有一个 Thread 1: signal 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 控件连接到 Storyboard的 @IBOutlet 旁边),您需要打开 .swift文件和 Storyboard,尝试这些以确保其实际连接

  • 打开 Storyboard文件,然后打开 MapScreen.swift文件中,如果已连接,则应填充连接器
  • 打开 Storyboard 文件并单击以显示 Assistant Editor所以 Storyboard和 MapScreen.swift文件是并行打开的,还要确保将 Class 设置为 MapScreenIdentity 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:)内收到位置更新,为了能够在 map 上查看用户位置,我们需要将 map 区域设置为该位置,使用 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

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

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