ios - 在 Swift 中将两个参数传递给 NSTimer

标签 ios swift arguments nstimer

我有一个在 TextView 中包含文本字符串的函数。我想随着时间的推移改变该文本的褪色。这不是我怀疑的褪色程度的实现,而是如何将两个参数(alpha 值和应该褪色的字符范围)传递给 NSTimer 中的选择器。

我看过this问题,但这并没有为我提供答案。

这是我现在拥有的:

func someFunc(){

    var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("val:"), userInfo: 5, "someString", repeats: true)

}


func val(val1: Int, val2: String){

    println("printing \(val1) and \(val2)")

}

但是它给了我一个“调用中的额外参数‘选择器’”错误。我需要能够传递两个参数,但我也不能正确传递一个参数;从函数中删除 val2 并删除“someString”,因此我只传递一个参数,导致函数在每个时间步打印“打印 140611230609088”行。

最佳答案

用您的对象创建一个数组并将其发送到 userInfo。试试这个:

func someFunc(){
    var arr = ["one", "two"] 
    var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("val:"), userInfo: arr, repeats: true)

}


func val(timer: NSTimer){

    //You'll get an array in timer.userInfo
    // arr = timer.userInfo.
    // Now you'll get you data in each index of arr

}

希望这会有所帮助..:)

关于ios - 在 Swift 中将两个参数传递给 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25501121/

相关文章:

ios - 方向改变后缩放 UIImageView

ios - 有没有像 google Place Search API 这样的 Apple API?

swift - 双泛型数据结构上的 flatMap 是什么样子的?

ios - 协议(protocol)中的静态变量

arguments - 批处理模式下的Stata命令行参数

c++ - 传递一个函数(具有可变数量的参数!)作为参数

java - IIS 中的 JNLP 参数

ios - SWRevealViewController 右侧菜单

ios - 在 Objective C 中创建简单的可变数组

Swift:UIPanGestureRecognizer 反向滑动错误