Passing parameters to a method called by NSTimer in Swift

The selector you use with NSTimer is passed the NSTimer object as it’s one and only parameter. Put the circle object in it as userInfo and you can extract it when the timer fires.

var circle = Circle()
var timer = NSTimer.scheduledTimerWithInterval(1.0, target: self, selector: "animate:", userInfo: circle, repeats: true)

func animate(timer:NSTimer){
  var circle = timer.userInfo as Circle
  //do stuff with circle
}

Leave a Comment