ViewController.Type has no member named

Just a simple task, but I have problems. Trying to do another way, but it fails.

enter image description here

How to start NSTimer with a previously declared variable? Neither var nor let helps.

+5
variables xcode swift nstimer
Sep 15 '14 at 19:08
source share
2 answers

The initial value of the property (in your case: timer ) cannot depend on another property of the class (in your case: interval ).

Therefore, you need to move the timer = NSTimer(interval, ...) setting to a class method, for example. in viewDidLoad . As a result, timer should be defined as optional or implicitly deployed optional.

Note also that Selector(...) takes a literal string as an argument, not the method itself.

So this should work:

 class ViewController: UIViewController { var interval : NSTimeInterval = 1.0 var timer : NSTimer! func timerRedraw() { } override func viewDidLoad() { super.viewDidLoad() timer = NSTimer(timeInterval: interval, target: self, selector: Selector("timerRedraw"), userInfo: nil, repeats: true) // ... } // Other methods ... } 
+10
Sep 15 '14 at 19:31
source share

Try:

 var interval:NSTimeInterval = 1.0 var timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "timerRedraw:", userInfo: nil, repeats: true) 

pro-tip and hopefully appreciated by FYI: Swift functions should also start with lowercase letters (ie " timerRedraw ").

0
Sep 15 '14 at 19:16
source share



All Articles