如何在计时器选择器上传递参数 [英] How do I pass params on timer selector
问题描述
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let mostRecentLocation = locations.last else {
return
}
print(mostRecentLocation.coordinate.latitude)
print(mostRecentLocation.coordinate.longitude)
Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(StartTestVC.sendDataToServer), userInfo: nil, repeats: true)
}
func sendDataToServer (latitude: Double, longitude: Double) {
SFUserManager.shared.uploadPULocation(latitude, longitude:longitude)
}
我想每 1 分钟向服务器发送一次数据.我正在使用 Timer.scheduledTimer 和设置选择器.但是我怎么能向我的函数发送 lat/lng 参数呢?
I want send data to server every 1 minute. I am using Timer.scheduledTimer and setting selector. But how could I send lat/lng params to my function?
推荐答案
对于使用 Timer
发送数据,您可以使用 userInfo
参数来传递数据.
For sending the data with Timer
you can use the userInfo
parameter for pass the data.
这是您可以调用选择器方法并将您的位置坐标传递给它的示例.
Here is the sample by which you can get call of selector method and by that you can pass your location coordinate to it.
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(iGotCall(sender:)), userInfo: ["Name": "i am iOS guy"], repeats:true)
要处理那个 userInfo
,你需要按照下面的方法去做.
For handling that userInfo
you need to go according to below.
func iGotCall(sender: Timer) {
print((sender.userInfo)!)
}
对于您的情况,请确保您的 didUpdateLocations
经常被调用.
for your case make sure your didUpdateLocations
is called frequently.
这篇关于如何在计时器选择器上传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!