title: Why does [weak self] work but [unowned self] break in a Swift closure?
body: <p>This SpriteKit action repeats by calling itself with a completion closure.  It uses a closure, rather than an <code>SKAction.repeatActionForever()</code>, because it needs to generate a random variable each repetition:</p>
  
  <pre><code>class Twinkler: SKSpriteNode {
    init() {
      super.init(texture:nil, color:UIColor.whiteColor(), size:CGSize(width:10.0, height:10.0))
      twinkle()
    }
    func twinkle() {
        let rand0to1 = CGFloat(arc4random()) / CGFloat(UINT32_MAX)
        let action = SKAction.fadeAlphaTo(rand0to1, duration:0.1)
        let closure = {self.twinkle()}
        runAction(action, completion:closure)
    }
  }
  </code></pre>
  
  <p>I think I should be using <code>[unowned self]</code> to avoid a strong reference cycle with the closure. When I do that:</p>
  
  <pre><code>let closure = {[unowned self] in self.twinkle()}
  </code></pre>
  
  <p>It crashes with the error: <code>_swift_abortRetainUnowned</code>.  But if I use <code>[weak self]</code> instead:</p>
  
  <pre><code>let closure = {[weak self] in self!.twinkle()}
  </code></pre>
  
  <p>It executes without error.  Why would <code>[weak self]</code> work but <code>[unowned self]</code> break?  Should I even be using either of these here?</p>
  
  <p>The <code>Twinkler</code> object is strongly referenced elsewhere in the program, as a child of another node.  So I don't understand how the <code>[unowned self]</code> reference is breaking.  It shouldn't be deallocated.</p>
  
  <p>I tried replicating this problem outside SpriteKit using <code>dispatch_after()</code>, but I was unable to.</p>