运行 我在 Swift 10.2 上的自编码应用程序有问题
I have a problem on running my self coded app on Swift 10.2
Xcode 表示有一个 Thread 1: signal SIGABRT 。它还说 libc++abi.dylib: 终止与 NSException 类型的未捕获异常
(LLDB)。我是初学者所以请回复 "easily" ;)
// 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
文件和情节提要,尝试使用这些来制作确保其实际连接
- 打开storyboard文件,然后打开
MapScreen.swift
文件,连接器需要填写
- 打开故事板文件并单击以显示
Assistant Editor
因此故事板和 MapScreen.swift
文件同时打开,还要确保在 [= 中将 Class 设置为 MapScreen
18=],如下图所示
我还会列出一些关于 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
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: { [=11=].timestamp < .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)
}
}
产量
Xcode 表示有一个 Thread 1: signal SIGABRT 。它还说 libc++abi.dylib: 终止与 NSException 类型的未捕获异常 (LLDB)。我是初学者所以请回复 "easily" ;)
// 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
文件和情节提要,尝试使用这些来制作确保其实际连接
- 打开storyboard文件,然后打开
MapScreen.swift
文件,连接器需要填写 - 打开故事板文件并单击以显示
Assistant Editor
因此故事板和MapScreen.swift
文件同时打开,还要确保在 [= 中将 Class 设置为MapScreen
18=],如下图所示
我还会列出一些关于 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
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: { [=11=].timestamp < .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)
}
}
产量