title: How to pass callback functions in Swift
body: <p>I have a simple class which <code>init</code> method takes an Int and a callback function.</p>
  
  <pre><code>class Timer {
    var timer = NSTimer()
    var handler: (Int) -&gt; Void
  
    init(duration: Int, handler: (Int) -&gt; Void) {
        self.duration = duration
        self.handler = handler
        self.start()
    }
    @objc func someMethod() {        
        self.handler(10)
    }
  }
  </code></pre>
  
  <p>Then in the ViewController I have this:</p>
  
  <pre><code>var timer = Timer(duration: 5, handler: displayTimeRemaining)
  func displayTimeRemaining(counter: Int) -&gt; Void {
      println(counter)
  }
  </code></pre>
  
  <p>This doesn't work, I get the following:</p>
  
  <p><code>'Int' is not a subtype of 'SecondViewController'</code></p>
  
  <p><strong>Edit 1: Adding full code.</strong></p>
  
  <p>Timer.swift</p>
  
  <pre><code>import UIKit
  
  class Timer {
      lazy var timer = NSTimer()
      var handler: (Int) -&gt; Void
  
      let duration: Int
      var elapsedTime: Int = 0
  
      init(duration: Int, handler: (Int) -&gt; Void) {
          self.duration = duration
          self.handler = handler
          self.start()
      }
  
      func start() {
          self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
              target: self,
              selector: Selector("tick"),
              userInfo: nil,
              repeats: true)
      }
  
      func stop() {
          timer.invalidate()
      }
  
      func tick() {
          self.elapsedTime++
  
          self.handler(10)
  
          if self.elapsedTime == self.duration {
              self.stop()
          }
      }
  
      deinit {
          self.timer.invalidate()
      }
  }
  </code></pre>
  
  <p>SecondViewController.swift</p>
  
  <pre><code>import UIKit
  
  class SecondViewController: UIViewController {
  
      @IBOutlet var cycleCounter: UILabel!
      var number = 0
  
      var timer = Timer(duration: 5, handler: displayTimeRemaining)
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      @IBAction func btnIncrementCycle_Click(sender: UIButton){
          cycleCounter.text = String(++number)
          println(number)
      }
  
      func displayTimeRemaining(counter: Int) -&gt; Void {
          println(counter)
      }
  }
  </code></pre>
  
  <p>I just started with Swift so I'm very green. How are you supposed to pass callbacks? I've looked at examples and this should be working I think. Is my class defined incorrectly for the way I'm passing the callback?</p>
  
  <p>Thanks</p>