【问题标题】:How to avoid using two extensions with the same method for 2 different classes如何避免对 2 个不同的类使用具有相同方法的两个扩展
【发布时间】:2019-07-25 02:33:38
【问题描述】:

我想知道处理这种情况的最佳方法是什么。我有两个不同的视图控制器,它们都将使用来自 CLlocationManagerDelegate 的相同 didUpdateLocations 方法。我对它们都使用了扩展,并使它们符合 CLManagerDelegate。我想知道是否有另一种方法可以获得相同的结果。感谢所有的解释和回复。

第一个视图控制器


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

第二个视图控制器

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)")

        }
    }
}

我正在考虑下面的代码,但我不知道它是否比以前的更好,并为第二个视图控制器做同样的事情。

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

【问题讨论】:

    标签: ios swift swift-protocols swift-extensions


    【解决方案1】:

    我认为您的问题中有一个错字,因为SecondViewController 的代码使用FirstViewController 作为名称。

    如果我理解正确,您有两个符合CLLocationManagerDelegate 的视图控制器,并且两者都重复了相同的代码。如果这就是您要解决的问题,我的建议是创建一个符合CLLocationManagerDelegateBaseViewController,然后让您的ViewControllers 继承自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
    }
    

    【讨论】:

    • 是的,我想避免两个相同的代码被重复。我现在知道了。所以我不需要涉及协议等。
    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 2011-04-21
    • 2012-11-18
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多