【发布时间】:2019-08-23 03:33:21
【问题描述】:
Xcode 说有一个 Thread 1: signal SIGABRT 。它还说 libc++abi.dylib: terminating with uncaught exception of type 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
}
}
【问题讨论】:
-
您是否连接了情节提要中的
mapView插座? (你在 Xcode 中看到属性旁边的实心圆圈了吗?)。你看到位置权限提示了吗?您是否已将位置使用说明添加到您的info.plist?应该有与控制台中显示的异常相关的错误消息 -
@Paulw11 是的,除了实心圆圈外,一切似乎都很好。它没有被填满。但是我连接了它...
-
您收到错误消息,因为您告诉应用程序在地图上显示用户的当前位置。但该应用尚未准备好这样做,因为您不允许该应用访问地图视图的委托属性。
-
@ElTomato 我该如何解决这个问题?
-
搜索 showUserLocation 并阅读其他主题。