CLLocationManager和CLLocationManagerDelegate的问题

时间:2018-10-08 21:19:23

标签: ios swift core-location cllocationmanager

我正在运行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 个答案:

答案 0 :(得分:2)

由于您的{@DatePeriod} in Date({?Pm-@DatePeriod})类是公共的,因此委托方法也必须是公共的。只需在三个委托方法之前添加PhysicalLocationManager,警告就会消失。