title: Swift, Observer pattern with closures
body: <p>I'm trying to implement the Observer pattern in swift using only functions:</p>
  
  <pre><code>var closures: [() -&gt; Void] = []
  
  class A: NSObject
  {
      static var c = 0
      var i = 0
  
      override init()
      {
          super.init()
          self.i = A.c
          A.c += 1
      }
  
      func foo()
      {
          print("Hi: \(i)")
          print("\(A.c)")
      }
  } // class
  
  var aa:A? = A()
  
  closures.append(aa!.foo)
  
  for item in closures
  {
      item()
  }
  
  aa = A()
  
  for item in closures
  {
      item()
  }
  </code></pre>
  
  <p>this prints:</p>
  
  <pre><code>Hi: 0
  1
  Hi: 0
  2
  </code></pre>
  
  <p>First question, it looks like the instance variable <code>i</code>  is never modified, do you know why?</p>
  
  <p>Second question, will it leak memory? Since I have an array of functions, will <code>aa</code>ever be released without emptying the array?</p>
  
  <p>Third question, any better idea for an Observe pattern using just functions? (I don't want to use protocols)</p>