Ios 传递函数的参数作为scheduledTimer的选择器

Ios 传递函数的参数作为scheduledTimer的选择器,ios,swift,timer,selector,nstimer,Ios,Swift,Timer,Selector,Nstimer,因此,我在按下按钮(在函数中)时启动一个定时计时器 我想在选择器中传递用于更新的参数数据,因为我需要数据,但它仅在函数中可用,但我不能在此函数中声明函数并将其用作#选择器 因此,理想情况下: func update(data: CMMotionActivity) { some code here } 在计时器中,作为选择器的不是ViewController.update,而是有点像ViewController.update(数据:myAwesomeArray)您可以使用userInfo sel

因此,我在按下按钮(在函数中)时启动一个定时计时器

我想在选择器中传递用于更新的参数数据,因为我需要数据,但它仅在函数中可用,但我不能在此函数中声明函数并将其用作#选择器

因此,理想情况下:

func update(data: CMMotionActivity) { some code here }
在计时器中,作为选择器的不是ViewController.update,而是有点像ViewController.update(数据:myAwesomeArray)

您可以使用userInfo

self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: Selector(ViewController.update(_:)), userInfo: ["data" : youData], repeats: true)
并将其纳入您的函数中:

func update(data: Timer) {
   let userInfo = timer.userInfo as Dictionary<String, AnyObject>
   let data = userInfo["data"]
   //use your data
}
func更新(数据:计时器){
让userInfo=timer.userInfo作为字典
让数据=用户信息[“数据”]
//使用您的数据
}

如果有助于解决问题,请不要忘记接受答案;-)
func update(data: Timer) {
   let userInfo = timer.userInfo as Dictionary<String, AnyObject>
   let data = userInfo["data"]
   //use your data
}