【问题标题】:Issue with CLLocationManager and CLLocationManagerDelegateCLLocationManager 和 CLLocationManagerDelegate 的问题
【发布时间】:2019-03-13 14:18:13
【问题描述】:

我正在运行 Xcode 10 和 iOS 12

我的 CLLocationManager 单例的类扩展中编码的每个委托方法都收到此警告:

实例方法'locationManager(:didChangeAuthorization:)' 几乎匹配协议'CLLocationManagerDelegate'的可选要求'locationManager(:didChangeAuthorization:)'

代码如下:

import Foundation
import CoreLocation

public class PhysicalLocationManager: NSObject {

    /*--------------------------------------------------------------------------------*/
    //MARK: - Create Singleton Shared Instance
    /*--------------------------------------------------------------------------------*/
    static let sharedInstance: PhysicalLocationManager = {
        let instance = PhysicalLocationManager()
        return instance
    }()

    let locationMgr: CLLocationManager

    /*--------------------------------------------------------------------------------*/
    //MARK: - Initialization
    /*--------------------------------------------------------------------------------*/
    override init() {
        locationMgr = CLLocationManager()
        locationMgr.distanceFilter = kCLDistanceFilterNone
        locationMgr.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        super.init()
        locationMgr.delegate = self
    }

    func enableBasicLocationServices() {
        switch CLLocationManager.authorizationStatus() {
        case .notDetermined:
            // Request when-in-use authorization initially
            locationMgr.requestWhenInUseAuthorization()
            break

        case .restricted, .denied:
            // Disable location features
            // TODO: disableMyLocationBasedFeatures()
            break

        case .authorizedWhenInUse, .authorizedAlways:
            // Enable location features
            enableWhenInUseFeatures()
            break
        }
    }

    func enableWhenInUseFeatures() {
        locationMgr.startUpdatingLocation()
        if CLLocationManager.locationServicesEnabled() {
            locationMgr.requestLocation()
        }
    }

}

extension PhysicalLocationManager: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        print("\(manager)\tCLLocationManager, didChangeAuthorization\n\(status)")

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("\(manager)\tCLLocationManager, didUpdateLocations\n\(locations)")
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    //  locationManager.stopUpdatingLocation()
    }

}

谁能看到我在这里做错了什么?

谢谢,

【问题讨论】:

  • 1.请修复您的代码格式。 2. 你用的是哪个版本的 Swift 和 Xcode?​​span>
  • 已修复并在问题中添加了 xCode 10 和 iOS 12。你知道怎么回事吗?

标签: ios swift core-location cllocationmanager


【解决方案1】:

因为您的 PhysicalLocationManager 类是公共的,所以委托方法也需要是公共的。只需在三个委托方法前添加public,警告就会消失。

【讨论】:

  • 解决了最初的问题 - 谢谢!
  • 现在我正在执行 didFailWithError 方法但没有填充错误 "error = Error Domain=kCLErrorDomain Code=0 "(null)"
  • 那将是一个新问题。请务必发布所有相关详细信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
  • 2014-08-31
  • 2011-11-23
  • 1970-01-01
相关资源
最近更新 更多