0
votes

I'm running Xcode 10 and iOS 12

I'm getting this warning on each of the delegate methods coded in the class extension of my CLLocationManager singleton:

Instance method 'locationManager(:didChangeAuthorization:)' nearly matches optional requirement 'locationManager(:didChangeAuthorization:)' of protocol 'CLLocationManagerDelegate'

Here is the code:

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

}

Can anyone see what I am doing wrong here?

Thanks,

1
1. Please fix your code formatting. 2. Which version of Swift and Xcode are you using? - rmaddy
Fixed and added xCode 10 and iOS 12 to the question. Do you know what is wrong? - Rob

1 Answers

2
votes

Because your PhysicalLocationManager class is public, the delegate methods need to be public as well. Simply add public in front of the three delegate methods and the warnings go away.