gpt4 book ai didi

ios - Swift 4.1 中的 locationManager 展开错误,但 Swift 4.0 中没有

转载 作者:行者123 更新时间:2023-11-30 11:35:49 25 4
gpt4 key购买 nike

我在 Swift 4.0 中编写了代码,允许我正在开发的应用程序显示带有目的地和用户位置的 map 。现在,在最近更新 Swift 4.1 之前,该应用程序可以完美运行。

错误消息表明在解包可选值时意外发现 nil。在 xCode 输出窗口中,它显示以下文本:

无法从角 4 插入合法归属(lldb)

我尝试了各种各样的方法,并在互联网上查找了这个问题,我不确定这是否是 Swift 4.1 中的一个错误,或者我需要做一些新的事情。就像说的,这个应用程序以前运行得很好。

我还确保 info.plist 具有适用于位置管理器的正确隐私属性。

Info.plist

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

最佳答案

您应该检查 sourceCooperatives 是否为零。

例如

guard let sourceCoordinates = locationManager.location?.coordinate else {
return
}

关于ios - Swift 4.1 中的 locationManager 展开错误,但 Swift 4.0 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785335/

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