快速使用Map时启用了金属API验证

问题描述 投票:0回答:1

我正在使用位置显示功能

当运行我的应用程序时,它向我显示此消息->

启用金属API验证致命错误:意外地发现nil,同时隐式展开一个可选值:file

这是我的代码

import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
    @IBOutlet weak var mymap: MKMapView!
    var locationManager: CLLocationManager!

    override func viewDidLoad() {
          checkLocationSevices()
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func setupLocationManger()
    {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }

    func checkLocationSevices()
    {
        if CLLocationManager.locationServicesEnabled()
        {
            setupLocationManger()
            checkLocationAuthorizatiob()
        }
        else
        {

        }
    }
    func checkLocationAuthorizatiob()
    {
        switch CLLocationManager.authorizationStatus()
        {
        case .authorizedWhenInUse:
            mymap.showsUserLocation = true
            break
        case .denied:
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            break
        case .authorizedAlways:
            break

         default:
            break
        }
    }
}

我也添加了ti info.plist->info.plist这是我得到的错误图像myBuild

ios swift xcode mapkit allocation
1个回答
0
投票
locationManager = CLLocationManager()

因为它就像-> var locationManager: CLLocationManager!

所以locationManager的值将是,但是当添加-> locationManager = CLLocationManager()->时,您将获得locationManager的值

import UIKit import MapKit import CoreLocation class ViewController: UIViewController,CLLocationManagerDelegate { @IBOutlet weak var mymap: MKMapView! var locationManager: CLLocationManager! override func viewDidLoad() { locationManager = CLLocationManager() checkLocationSevices() super.viewDidLoad() // Do any additional setup after loading the view. } func setupLocationManger() { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest } func checkLocationSevices() { if CLLocationManager.locationServicesEnabled() { setupLocationManger() checkLocationAuthorizatiob() } else { } } func checkLocationAuthorizatiob() { switch CLLocationManager.authorizationStatus() { case .authorizedWhenInUse: mymap.showsUserLocation = true break case .denied: break case .notDetermined: locationManager.requestWhenInUseAuthorization() break case .restricted: break case .authorizedAlways: break default: break } } }

© www.soinside.com 2019 - 2024. All rights reserved.