title: How to do View Controller callback in Swift
body: <p>I am trying to do a callback from a child view controller back to its parent. The value passed back is printed successfully but I then get an <code>EXC_BAD_INSTRUCTION</code>.</p>
  
  <p>In the parent view controller I construct the child controller like so:</p>
  
  <pre><code>let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  if let vc = storyboard.instantiateViewControllerWithIdentifier("dataEntryView") as? DataEntryViewController {
      dataEntryVC = vc
      vc.callback = calculateHeartRate
      self.presentViewController(dataEntryVC!, animated: true, completion: nil)
  }
  </code></pre>
  
  <p>I call back from the child controller like this:</p>
  
  <pre><code>@IBAction func done(sender : AnyObject) {
      dismissModalViewControllerAnimated(true)
      if let cb = callback {
          cb(beatCount)
      }
  }
  </code></pre>
  
  <p>To this function in the parent controller:</p>
  
  <pre><code>func calculateHeartRate(beats : Int?) {
      println("Beats = \(beats)")
  
      if beats {
          let heartrate = 60/secondsCount * beats!
  
          println("Heart rate \(heartrate)")
      }
  }
  </code></pre>
  
  <p>The println correctly outputs to the console and then I get an exception on the "if beats {" line.</p>
  
  <p>Is there something inherently wrong with doing a callback like this?</p>