如何在计时器选择器上传递参数 [英] How do I pass params on timer selector

查看:25
本文介绍了如何在计时器选择器上传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆