Reputation: 462
I have a function that contains strings of text in a TextView. I would like to change the fading of that text over time. It is not the implementation of how fading I'm in doubt about, but rather how to pass two arguments (the alpha value and the range of characters that should be faded) to the Selector in the NSTimer.
I have looked at this question, but that does not provide me with an answer.
This is what I have right now:
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)")
}
However it gives me an "Extra argument 'selector' in call" error. I need to be able to pass two arguments, but I can't pass a single one correctly either; removing val2 from the function and removing "someString", so I only pass one argument, results in the function printing the line "printing 140611230609088" at every time step.
Upvotes: 12
Views: 13520
Reputation: 864
I have modified your code as following.
I have created a NSTimer! variable which will be assigned in someFunc() by method scheduledTimerWithTimeInterval.
then I have set selector as String type (recommended syntax in Swift)
After this I have sent the userInfo which will be retrieved using timer.userInfo in val()
I hope this will help you to solve your problem.
var timer :NSTimer!
func someFunc(){
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "val", userInfo:"Userinfo", repeats: true)
}
func val(){
println("printing \(timer?.userInfo) ")
}
Note: You can send anything in userInfo. But, off course you have to retrieve the userInfo using timer.userInfo
Upvotes: 0
Reputation: 11197
Make a array with your objects and send it to userInfo. Try this:
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
}
Hope this helps.. :)
Upvotes: 30
Reputation: 93
First of all, you've got too many arguments. Secondly, the func receives an NSTimer so you either have to use AnyObject or NSTimer.
So my suggetion would be to use a dictionary or an array and pass that as the userInfo argument like so:
func someFunc(){
var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("val:"), userInfo: ["key1" : 1 , "key2" : "aString"], repeats: true)
}
func val(val : NSTimer?) {
println("printing \(val?.userInfo)")
}
Upvotes: 5