How to use NSTimer to fire off multiple times?

2019-03-04 03:29发布

问题:

I have a RNG and want it to go off every three seconds. So far I have

var timer = NSTimer(timeInterval: 3, target: self , selector: randomnumbers, userInfo: nil, repeats: true)

func randomnumbers() {
    var rockNamesArray:[String] = ["bird", "rock2", "rock3"]

    var rockpos = Int(arc4random_uniform(UInt32(3)))
}

But I have a bunch of error messages and I'm not sure how to organize it.

EDIT

The error message in this code is telling me that it has an unresolved identifier "self" and all the other error s are just ones occuring because I have changed this code, like unresolved identifier rockNamesArray and rockpos which happen 4 times in three different lines of code.

EDIT2

As stated in the comment the above code is placed outside of a class which explains that self is not working. But how to address the timer routine in this case?

回答1:

it has an unresolved identifier "self"

It sounds like the code you've provided is not part of an instance method. self is a keyword that refers to the object whose code is executing. If you don't have an object, there's no self, hence the error. To solve the problem, you could pass a pointer to some other object that has a randomnumbers selector in place of self.



回答2:

You can not refer to self as a value when assigning an initial value to every stored property before the completion of the first phase of initialization in Swift.

As The Swift Programming Language says:

Class initialization in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.

An initializer cannot call any instance methods, read the values of any instance properties, or refer to self as a value until after the first phase of initialization is complete.



回答3:

Try this one:

var timer = NSTimer(timeInterval:3, target:self, selector:Selector("randomnumbers:"), userInfo: nil, repeats: true)

func randomnumbers(timer:NSTimer) {
    var rockNamesArray:[String] = ["bird", "rock2", "rock3"]

    var rockpos = Int(arc4random_uniform(UInt32(3)))
}

since the timer routine expects a timer object.

Edit You need to place it inside a (dummy) class like this:

class MyTimer {
  var timer: NSTimer
  init() {
    timer = NSTimer(timeInterval:3, target:self , selector:Selector("randomnumbers:"), userInfo:nil, repeats:true)
  }

  func randomnumbers(timer:NSTimer) {
    // ...
  }
}

let myTimer = MyTimer()


回答4:

Write selector: "randomnumbers" instead of selector: randomnumbers. You can instantiate the timer and start it at the same time using timer = NSTimer.scheduledTimerWithTimeInterval instead of timer = NSTimer(timeInterval.... Call this in your controller, e.g. in viewDidLoad

Please also note that your randomnumbers() does not do anything. You assign value to rockpos, but do not do anything with it. So you won't be able to see if the timer is working...



标签: swift nstimer