Swift nstimer 访问其他函数 class

Swift nstimer accessing function other class

遇到问题 运行将不同 class 中的函数设置为 NStimer 的选择器。

class VariClass
{
  func updateUILoop()
    {
         print("updating UI")
    }
}

在我的应用委托中

let values = VariClass()

在我的 appdelegate 中。

let timer = NSTimer(fireDate: NSDate(), interval: 5, target: self, selector: #selector(values.updateUILoop), userInfo: nil, repeats: true)

如果我 运行 手动操作,该功能将完美运行。

getvalues.updateUILoop()

您希望 NSTimervalues 对象上调用 func updateUILoop,因此计时器回调的目标应该是 values,而不是 self:

let timer = NSTimer(fireDate: NSDate(), interval: 5, target: self.values, selector: #selector(VariClass.updateUILoop), userInfo: nil, repeats: true)