ios - 如何在定时器选择器上传递参数

标签 ios swift swift3 core-location

<分区>

  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 参数发送到我的函数呢?

最佳答案

要使用 Timer 发送数据,您可以使用 userInfo 参数来传递数据。

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

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

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

func iGotCall(sender: Timer) {
    print((sender.userInfo)!)
}

对于您的情况,请确保您的 didUpdateLocations 被频繁调用。

关于ios - 如何在定时器选择器上传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285251/

相关文章:

ios - 对 <ViewController> 开始/结束外观转换的不平衡调用

ios - 任何人都可以在模拟器中运行的 xcode 构建/存档应用程序

swift - 如何实现多个PanGestures(Draggable views)?

ios - 翻转后 UIView 的框架发生变化

windows - 据我所知,你不能在Windows上编译Swift,那么SwiftStub如何在Windows上编译代码呢?

Swift 2 向协议(protocol)添加协议(protocol)一致性

ios - Swift:无法使用 'filter' 类型的参数列表调用 '((Any) throws -> Bool)'

ios - 如何使用 firebase 在 xcode 中验证电子邮件和密码

iphone - 在不使用 Interface Builder 的情况下指定 CocoaTouch 控件坐标和大小

iphone - applicationDidBecomeActive中如何判断是否为iPhone应用初始启动?