title: Make self weak in methods in Swift
body: <p>I have a Swift class that needs to store a table of its own methods. Unfortunately this is causing a reference cycle, because its table retains references to <code>self</code> via the methods it stores.</p>
  
  <p>Example leaky code below:</p>
  
  <pre><code>typealias Callback = ()-&gt;()
  
  class CycleInducingClass : NSObject {
      var myCallbacks = [Callback]()  
  
      override init() {
          super.init()
          myCallbacks.append(myInternalFunction)
      }
  
      func myInternalFunction() {
          NSLog("lolol: %d", self.myCallbacks.count)
      }
  }
  </code></pre>
  
  <p>The only solution I've found so far is to instead do this:</p>
  
  <pre><code>myCallbacks.append({[unowned self] in self.myInternalFunction()})
  </code></pre>
  
  <p>That's pretty ugly, and prone to error. Any better ideas? Is there some trick for making the function references themselves be weak? i.e. to make the <code>myCallbacks</code> array of type <code>myCallbacks : [WeakCallback]()</code> or something? As far as I can tell I can't even build a convenience function <code>weaken</code> as syntactic sugar over the ugly closure wrapper above.</p>