gpt4 book ai didi

swift - 如何延迟方法调用,直到存储有关更新当前位置的变量值

转载 作者:行者123 更新时间:2023-11-30 13:46:19 30 4
gpt4 key购买 nike

我想调用一个方法,但它需要尚未存储在变量中的坐标。

到目前为止我有:1)获取当前位置2)你认为我存储了它们吗?

我想做的事:1)调用方法这些变量存储完毕后程序就可以运行了

class ViewController: UIViewController, CLLocationManagerDelegate  {

let locationManager = CLLocationManager()
var clLatitude : CLLocationDegrees!
var clLongitude: CLLocationDegrees!

override func viewDidLoad() {
super.viewDidLoad()

self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()


}



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location:CLLocationCoordinate2D = manager.location!.coordinate

//Test printing coordinates to screen
print("locations = \(location.self.clLatitude) \(location.self.clLongitude)")

//place where I think I store the variables?
self.clLatitude = location.latitude
self.clLongitude = location.longitude

}

func methodToBeCalled(){
//to be called after variables are stored.
}

我相信我已经涵盖了有关我的问题的所有内容

最佳答案

我认为你只需要调用 locationManager(manager: didUpdateLocations:) 末尾的方法

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location:CLLocationCoordinate2D = manager.location!.coordinate

//Test printing coordinates to screen
print("locations = \(location.self.clLatitude) \(location.self.clLongitude)")

//place where I think I store the variables?
self.clLatitude = location.latitude
self.clLongitude = location.longitude

// call method
methodToBeCalled()
}

func methodToBeCalled(){
//to be called after variables are stored.
}

关于swift - 如何延迟方法调用,直到存储有关更新当前位置的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884258/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com