bhzag
bhzag

Reputation: 2980

NSTimer 'time' issue

I have an NSTimerand I am using the method scheduledTimerWithTimeInterval but I would like the time interval it takes as a parameter to be a function, not an explicit value. How could I do this?

My code looks like this:

   timer = NSTimer.scheduledTimerWithTimeInterval(time(), target: self, selector: "doStuff", userInfo: nil, repeats: true)

The compiler complains with the first parameter time()

edit: the error is "extra argument 'selector' in call"

Upvotes: 1

Views: 463

Answers (1)

Aaron Golden
Aaron Golden

Reputation: 7102

I'm not sure exactly why you're getting that error, but time() is certainly not what you want for your time interval. That's supposed to be the number of seconds between firings of the timer. For something like a repeating animation timer that will typically be something like 1/30.0 or 1/60.0.

The time function (which, by the way, should take an argument) returns an integer that's the number of seconds since the "epoch." On Mac OS X the epoch is 12:00 AM, UTC of January 1, 2001.

Edit: It sounds from your comment like you've defined your own time function. I would first of all, change the name of your function to (say) timeInterval so it can't possibly collide with the system's time function. And I would make sure your function returns an NSTimeInterval, as opposed to a Float or some other number type, like:

func timeInterval() -> (NSTimeInterval) {
    return 1/60.0
}

var timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval(), target: self, selector: "doStuff", userInfo: nil, repeats: true)

Upvotes: 1

Related Questions