【发布时间】: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