首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWIFT4.1中的locationManager展开错误但SWIFT4.0中没有

SWIFT4.1中的locationManager展开错误但SWIFT4.0中没有
EN

Stack Overflow用户
提问于 2018-04-11 22:32:16
回答 1查看 169关注 0票数 0

我有自己在SWIFT4.0中编写的代码,它允许我正在开发的应用程序显示一个具有目的地和用户位置的Map。现在,这个应用程序在斯威夫特4.1的最新更新之前运行得很好。

错误消息显示,在展开可选值时,意外地发现为零。在xCode输出窗口中,它显示了以下文本:

无法从角4 (lldb)嵌入法律属性

我已经尝试过各种各样的事情,并在互联网上查找这个,我不确定这是否是Swift 4.1中的一个bug,还是有一些我需要做的新的事情。就像前面说的,这个应用程序以前运行得很好。

我还确保info.plist具有应用于Location的正确的隐私属性。

Info.plist

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-12 01:08:27

您应该检查sourceCoordinates是否为零。

例如:

代码语言:javascript
复制
guard let sourceCoordinates = locationManager.location?.coordinate else {
    return
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49785335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档