12

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.

Community
  • 1
  • 1
Benjamin Hviid
  • 463
  • 2
  • 5
  • 13

3 Answers3

30

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.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • I've tried you method and placed `println(timer.userInfo?.name)` in the `val`function. This writes nil whenever the function is run. – Benjamin Hviid Aug 26 '14 at 09:17
  • try printing timer.userInfo – Rashad Aug 26 '14 at 09:22
  • Printing `timer.userInfo` gives me this in it console: `Optional(<_TtCSs23_ContiguousArrayStorage00007FD413454E20 0x7fd413645890>( one, two ) ) ` How would I then access the individual values? I can't use `timer.userInfo[1]` for instance, it gives me an out of bounds error, so I suppose that it does not direct me to the array index. Assigning `arr`to `timer.userInfo` produces a warning (not error) that the inferred type of `arr` is of type AnyObject, which may be unexpected. – Benjamin Hviid Aug 26 '14 at 09:40
  • Why don't you take the timer.userInfo in an array??? like arr = timer.userInfo. Then try to access them by index??? – Rashad Aug 26 '14 at 09:42
  • I've edited that into my response. `var arr = timer.userInfo` behaves just like timer.userInfo, and I cannot access any of its indices. – Benjamin Hviid Aug 26 '14 at 09:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59990/discussion-between-rashad-and-benjamin-hviid-nielsen). – Rashad Aug 26 '14 at 09:47
  • When I use the the variables ("one","two") in your example, I'm working with a copy of the original data. However I want to pass the string from the TextView and make changes to it instead of a copy of it. Are there a way to pass the memory address of the string into the array, so I'm changing the displayed string? – Benjamin Hviid Aug 26 '14 at 17:15
5

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)")
}
DanielR
  • 93
  • 5
0

I have modified your code as following.

  1. I have created a NSTimer! variable which will be assigned in someFunc() by method scheduledTimerWithTimeInterval.

  2. then I have set selector as String type (recommended syntax in Swift)

  3. 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

Benjy Wiener
  • 1,085
  • 2
  • 9
  • 27
R_Developer
  • 864
  • 1
  • 7
  • 10
  • Where would I set `"Userinfo"` equal to my two variables, so that I can retrieve them in `val()`? – Benjamin Hviid Aug 26 '14 at 09:20
  • Create an array of these two values and pass that array with userInfo scheduledTimerWithTimeInterval. You can then retrieve that array using timer.userInfo – R_Developer Aug 26 '14 at 09:27