ViewController.Type does not have a member named
Asked Answered
I

2

6

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.

Invulnerable answered 15/9, 2014 at 19:8 Comment(1)
Possible duplicate of How to initialize properties that depend on each otherDogie
R
10

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 ...
}
Rossuck answered 15/9, 2014 at 19:31 Comment(0)
H
0

Try:

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

pro-tip and hopefully an appreciated FYI: Swift functions should also start with lower case letters (i.e. "timerRedraw").

Humiliate answered 15/9, 2014 at 19:16 Comment(2)
Unfortunately, it doesn't help. The same issue appears: interval not foundInvulnerable
yeah, I forgot to note that you need to put your two variables into your "viewDidLoad" method. Which is exactly what the other guy answering correctly said. I gave him a +1 for that.Humiliate

© 2022 - 2024 — McMap. All rights reserved.