Just a simple task, but I'm in trouble. Trying to make a different way but it fails.
How to init NSTimer with declared previously variable? Neither var nor let helps.
Just a simple task, but I'm in trouble. Trying to make a different way but it fails.
How to init NSTimer with declared previously variable? Neither var nor let helps.
The initial value of a property (in your case:
timer
) cannot depend on another property of the class (in your case:interval
).Therefore you have to move the assigment
timer = NSTimer(interval, ...)
into a method of the class, e.g. intoviewDidLoad
. As a consequence,timer
has to be defined as an optional or implicitly unwrapped optional.Note also that
Selector(...)
takes a literal string as argument, not the method itself.So this should work:
Try:
pro-tip and hopefully an appreciated FYI: Swift functions should also start with lower case letters (i.e. "
timerRedraw
").