title: How to used external class as target for NSTimer.scheduledTimerWithTimeInterval in Swift
body: <p>I have a Swift class i want to re-use for button animations.</p>
  
  <pre><code>//
  //  ButtonAnimation.swift
  
  import Foundation
  import UIKit
  
  
  @objc class ButtonAnimation : NSObject {
  
      let animationButtion :UIButton!
      let animationLastStep : Int
      let animationStepIncrement : Int
      let timer : NSTimer
      let baseName : String
      var animationStep: Int
  
  
      required init?(animationTimer: NSTimer) {
  
          self.timer = animationTimer
          let configuration: [String : AnyObject] = self.timer.userInfo as! [String : AnyObject]
  
          self.animationButtion =  configuration["Button"] as! UIButton
          self.animationLastStep = configuration["stop"] as! Int
          self.animationStep = configuration["start"] as! Int
          self.baseName = configuration["basefilename"] as! String
  
          if (self.animationLastStep &gt; self.animationStep) {
              self.animationStepIncrement = 1
          } else { self.animationStepIncrement = -1 }
  
      }
  
  
      @objc
      func animateButton() {
  
          self.animationStep += self.animationStepIncrement
  
          if (self.animationStep == self.animationLastStep) {
              self.timer.invalidate()
          }
  
          print("image \(self.baseName)\(String(format:"%02i",self.animationStep))")
  
          if let image = UIImage(named: String(self.baseName ) + String(format:"%02i",self.animationStep)) {
              self.animationButtion.setImage(image, forState: .Normal)
          }
  
      }
  
  }
  </code></pre>
  
  <p>This class is used from the viewController and invoked by the </p>
  
  <pre><code>    let dictionary: [String : AnyObject] = ["Button" : sortOnSeverityButton,
                                              "start": 0,
                                              "stop": 20,
                                              "basefilename": severityButtonAnimBasename]
  
      severityButtonTimer = NSTimer.scheduledTimerWithTimeInterval(severityButtonTimeInterval, target: ButtonAnimation.self , selector: #selector(ButtonAnimation.animateButton), userInfo: dictionary, repeats: true)
  </code></pre>
  
  <p>There are no errors when compiling the code, but gives an exception when executing the code starting the animation.</p>
  
  <blockquote>
    <p>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
    '+[homecaremonitor.ButtonAnimation animateButton]: unrecognized
    selector sent to class 0x103353c30'</p>
  </blockquote>
  
  <p>If i am interpreting the exception correct, there is something not right with the selector. But i am out of ideas how to resolve this.</p>
  
  <p>Any help is appreciated !
  Peter</p>
  
  <p>I now changed the invocation</p>
  
  <pre><code>        let buttonAnimation :ButtonAnimation = ButtonAnimation()
  
          severityButtonTimer = NSTimer.scheduledTimerWithTimeInterval(timeButtonTimeInterval, target: buttonAnimation , selector: #selector(ButtonAnimation.self.animateButton), userInfo: dictionary, repeats: true)
  </code></pre>
  
  <p>And moved the code initially in the initializer to the func animateButton</p>
  
  <pre><code>    if (self.timer == nil) {
  
          self.timer = animationTimer
              let configuration: [String : AnyObject] = self.timer.userInfo as! [String : AnyObject]
                  self.animationButton =  configuration["Button"] as! UIButton
                  self.animationLastStep = configuration["stop"] as! Int
                  self.animationStep = configuration["start"] as! Int
                  self.baseName = configuration["basefilename"] as! String
  
                  if (self.animationLastStep &gt; self.animationStep) {
                      self.animationStepIncrement = 1
                  } else { self.animationStepIncrement = -1 }
  
          self.startValue = self.animationStep
  
      }
  </code></pre>
  
  <p>It bugs me that i have to create an instance before creating the timer. When calling self as a Target, the NSTimer instance is passed to the initializer. When i want to use an external class this seems not to be possible. Therefore i have to create an initializer in the function called by the Selector.</p>