我有自己在SWIFT4.0中编写的代码,它允许我正在开发的应用程序显示一个具有目的地和用户位置的Map。现在,这个应用程序在斯威夫特4.1的最新更新之前运行得很好。
错误消息显示,在展开可选值时,意外地发现为零。在xCode输出窗口中,它显示了以下文本:
无法从角4 (lldb)嵌入法律属性
我已经尝试过各种各样的事情,并在互联网上查找这个,我不确定这是否是Swift 4.1中的一个bug,还是有一些我需要做的新的事情。就像前面说的,这个应用程序以前运行得很好。
我还确保info.plist具有应用于Location的正确的隐私属性。
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBAction func centerUserLocation(_ sender: Any) {
mapkitView.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)
}
@IBOutlet weak var mapkitView: MKMapView!
let locationManager = CLLocationManager()
@IBAction func phoneNumber(_ sender: Any) {
let number = "00436641491000"
if let url = URL(string: "tel://\(number)") {
UIApplication.shared.open(url)
}
}
@IBAction func emailAddress(_ sender: Any) {
let email = "salzburg@citybeats.at"
if let url = URL(string: "mailto:\(email)") {
UIApplication.shared.open(url)
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapkitView.delegate = self
mapkitView.showsScale = true
mapkitView.showsPointsOfInterest = true
mapkitView.showsUserLocation = true
mapkitView.userTrackingMode = .follow
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
let sourceCoordinates = locationManager.location?.coordinate
let destCoordinates = CLLocationCoordinate2DMake(47.800715, 13.041061)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let annotation = MKPointAnnotation()
annotation.coordinate = destCoordinates
annotation.title = "CityBeats"
annotation.subtitle = "House & RnB"
mapkitView.addAnnotation(annotation)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceItem
directionRequest.destination = destItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response, error in
guard let response = response else {
if let error = error {
print("Something went wrong!")
}
return
}
let route = response.routes[0]
self.mapkitView.add(route.polyline, level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.mapkitView.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.cyan
renderer.alpha = 0.7
renderer.lineWidth = 7.0
return renderer
}
}发布于 2018-04-12 01:08:27
您应该检查sourceCoordinates是否为零。
例如:
guard let sourceCoordinates = locationManager.location?.coordinate else {
return
}https://stackoverflow.com/questions/49785335
复制相似问题