Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewController.Type does not have a member named

Just a simple task, but I'm in trouble. Trying to make a different way but it fails.

enter image description here

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

like image 324
Olexiy Pyvovarov Avatar asked Sep 15 '14 19:09

Olexiy Pyvovarov


1 Answers

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. into viewDidLoad. 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:

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 ...
}
like image 180
Martin R Avatar answered Oct 26 '22 01:10

Martin R