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

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

问题描述

  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?

推荐答案

要使用计时器发送数据,您可以使用 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天全站免登陆