如何在计时器选择器上传递参数

pmb*_*pmb 3 core-location ios swift swift3

  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)
    }
Run Code Online (Sandbox Code Playgroud)

我想每1分钟将数据发送到服务器。我正在使用Timer.scheduledTimer和设置选择器。但是如何向我的函数发送经/纬度参数呢?

Mau*_*dya 5

为了与之一起发送数据,Timer可以使用userInfo参数传递数据。

这是您可以调用选择器方法并可以将位置坐标传递给它的示例。

Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(iGotCall(sender:)), userInfo: ["Name": "i am iOS guy"], repeats:true)
Run Code Online (Sandbox Code Playgroud)

要进行处理,userInfo您需要按照以下说明进行操作。

func iGotCall(sender: Timer) {
        print((sender.userInfo)!)
    }
Run Code Online (Sandbox Code Playgroud)

对于您的情况,请确保didUpdateLocations经常打电话给您。