Search code examples
iosswiftswift-protocolsswift-extensions

How to avoid using two extensions with the same method for 2 different classes


I would like to know what is the best way to deal with this situation. I have two different view controllers, both will use the same didUpdateLocations method from CLlocationManagerDelegate. I'm using an extensions for both of them and conform them to the CLManagerDelegate. I'm wondering if there is another way for the same result. Thanks for all explanation and replies.

First view controller


extension FirstViewController: CLLocationManagerDelegate{
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[locations.count - 1]
        
        if location.horizontalAccuracy > 0 {
            locationManager.stopUpdatingLocation()
            print("\(location.coordinate.longitude), \(location.coordinate.latitude)")
            
        }
    }
}

Second view controller

extension SecondViewController: CLLocationManagerDelegate{
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[locations.count - 1]

        if location.horizontalAccuracy > 0 {
            locationManager.stopUpdatingLocation()
            print("\(location.coordinate.longitude), \(location.coordinate.latitude)")

        }
    }
}

I was thinking to the following code, but I do not know if it better than the previous and do the same thing for the second view controller.

protocol localisation {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    
}

extension FirstViewController: localisation, CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[locations.count - 1]
        
        if location.horizontalAccuracy > 0 {
            locationManager.stopUpdatingLocation()
            print("\(location.coordinate.longitude), \(location.coordinate.latitude)")
        }
}

Solution

  • I think there is a typo in your question because the code for SecondViewController uses FirstViewController as the name.

    If I understand correctly, you have two view controllers that conform to CLLocationManagerDelegate with same code being repeated for both. If that's what you're looking to resolve, my suggestion would be to create a BaseViewController that conforms to CLLocationManagerDelegate and then have your ViewControllers inherit from BaseViewController.

    class BaseViewController: UIViewController {
        //Common code here
    }
    
    extension BaseViewController: CLLocationManagerDelegate{
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location = locations[locations.count - 1]
    
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                print("\(location.coordinate.longitude), \(location.coordinate.latitude)")
    
            }
        }
    }
    
    class FirstViewController: BaseViewController {
        //Your code here
    }
    
    class SecondViewController: BaseViewController {
        //Your code here
    }