0

Here is some sample code for what I am trying to do.

func firstFunction() {
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: Selector("secondFunction:"), userInfo: self.data!.getInfo(), repeats: false);
    println("Info: \(timer.userInfo)");
}

func secondFunction(value: Int) {
    println("Called with \(value)");
}

The following is the output: Info: Optional(( 2 )) and Called with 140552985344960

Called with ############ is constantly changing too. Even if I use just a number in place of self.data!.getInfo I still get Info: Optional(2) as the output and the Called with output still changes. I'm thinking it's happening because the value being passed is an optional one, so how do I make it not optional if that is the problem?

Ricca
  • 311
  • 4
  • 21
  • secondFunction should have an NSTimer as its parameter if you'd like to get the timer's user info... – Lyndsey Scott Dec 17 '14 at 03:34
  • I tried using an NSDictionary to store the data and it made no difference. I've seen other examples where userInfo was able to take single parameters as just the object. – Ricca Dec 17 '14 at 03:38

1 Answers1

3

NSTimer's scheduledTimerWithTimeInterval userInfo parameter is not a standard parameter in the sense that although you can set userInfo to hold AnyObject, you can't simply pass in a parameter as you would with most functions because scheduledTimerWithTimeInterval's selector can only be passed an NSTimer as its one and only parameter. So your secondFunction: must specify an NSTimer as its parameter if you'd like to access the value you have stored within the timer's userInfo, ex:

func firstFunction() {
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: Selector("secondFunction:"), userInfo: self.data!.getInfo(), repeats: false);
    println("Info: \(timer.userInfo)");
}

func secondFunction(timer: NSTimer) {
    var value = timer.userInfo as Int
    secondFunction(value)
}

// Function added based on your comment about
// also needing a function that accepts ints
func secondFunction(value: Int) {
    println("Called with \(value)");
}
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • self.data!.getInfo() is a generic function that I am calling, not the getInfo function for the NSTimer. I thought I was implying that when I showed that the output was `Info: Optional(2)`. So I don't want the NSTimer as a parameter but some other function's output. – Ricca Dec 17 '14 at 03:43
  • @Ricca Honestly, just try this code to see what I mean. The function's output will in fact be contained in the NSTimer' userInfo property as you're attempting to do. – Lyndsey Scott Dec 17 '14 at 04:04
  • @Ricca Then you need two separate functions accepting two separate parameters. I'll update to show you how. P.S. Again, you're not "supposed" to be able to pass the integer into the scheduledTimerWithTimeInterval selector function. – Lyndsey Scott Dec 17 '14 at 05:15
  • @Ricca Updated to show you how you could implement functions for BOTH the `NSTimer` `scheduledTimerWithTimeInterval` case and the function you apparently need to call elsewhere. – Lyndsey Scott Dec 17 '14 at 05:19
  • That looks like something I can work with. At least in my opinion I would expect to be able to pass in parameters like a function. Oh well. Thanks for the help. – Ricca Dec 17 '14 at 05:27