如何将整数变量参数传递给 NSTimer 调用函数

how to pass a integer variable argument to the NSTimer calling function

我想在 NSTimer 调用该函数时将一个具有整数值的变量传递给该函数

func work()
{
    var x= 10
    var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "Disp", userInfo: x , repeats: false)
}

    func Disp(timer:NSTimer)
    {
       //access the x value
       print("\(x)")
    }

但是 userinfo 部分只接受类型对象(AnyObject?)的参数。请有人帮助我。此外,变量 x 不应全局声明,因为 x 是一个局部变量,其值是在运行时动态分配的,在我最初需要的方法中。

这是我的确切情况。请帮助我的朋友。提前致谢。

你可以在调用NSTimer时传递用户信息中的值,当你的Disp函数被调用时你可以从定时器用户信息中获取值

 func work()
    {
        var x = 10
        var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "Disp:", userInfo: x , repeats: false)
    }


    func Disp(timer:NSTimer)
    {
        //access the x value
        print("\(timer.userInfo)")
    }