title: Why use delegate and protocol instead of just passing an instance in Swift?
body: <p>I was trying to pass around variables between views in Swift, and ran into the rather abstract concept of protocols and delegates. </p>
  
  <p>Then I tried storing a reference to the first view in a second view and call functions on that directly. This seems to work:</p>
  
  <p><em>SCREEN 1</em></p>
  
  <pre><code>class Screen1: UIViewController {
  
      var myName = "Screen1"
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
      //
      // checking if the segue to screen 2 is called and then passing a reference
      //
      override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
          if segue.identifier == "screen2Segue"{
              let vc = segue.destinationViewController as Screen2
              vc.storedReference = self
          }
      }
  
      func getName() -&gt; String {
          return myName
      }
  }
  </code></pre>
  
  <p><em>SCREEN 2</em></p>
  
  <pre><code>class Screen2: UIViewController {
  
      var storedReference:Screen1!
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
      func testReference() {
          // calling a function on the stored reference to screen 1
          var str = storedReference.getName()
          println("Leaving screen 2, going to " + str)
      }
  }
  </code></pre>
  
  <p>My question: what's wrong with this code? Why use delegates and protocols if you can just pass around a reference directly?</p>
  
  <p>Perhaps related: when does a view get un-initialized and replaced by an entirely new view instance? Am I calling 'getName()' on an old instance?</p>