title: Swift: AXUIElementCopyAttributeValue returns kAXErrorSuccess but got nil pointer
body: <p>please excuse me for my bad English! I'm trying to get the focused application in Mac OS X. The code below works fine when running in XCode 6.1.1:</p>
  
  <pre><code>func getFocusedApp() -&gt; AXUIElementRef? {
      let systemWideElement : AXUIElement = AXUIElementCreateSystemWide().takeRetainedValue()
      let kAXFocusedApplication : CFString! = "AXFocusedApplication"
      var focusedElementPtr : Unmanaged&lt;AnyObject&gt;?
      var error : AXError = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedApplication, &amp;focusedElementPtr)
      if let ue = focusedElementPtr {
          return ue.takeUnretainedValue() as AXUIElementRef
      }
      else {
          printError(error)
      }
      return nil
  }
  </code></pre>
  
  <p>in which <em>printError</em> is obviously a function to print out the error. However when I archive the app to put into Applications folder of Yosemite, <em>focusedElementPtr</em> is always nil while <em>error == AXError(kAXErrorSuccess)</em> (accessibility enabled, it passes the <em>AXIsProcessTrustedWithOptions</em> test). I couldn't figure out what's wrong. So it would be nice if someone can help me.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>UPDATE</strong>: I guessed it was related to memory management, so I changed the code to:</p>
  
  <pre><code>func getFocusedApp() -&gt; AXUIElementRef? {
      var output: AXUIElementRef? = nil
      let systemWideElement : AXUIElement = AXUIElementCreateSystemWide().takeRetainedValue()
      let kAXFocusedApplication : CFString! = "AXFocusedApplication"
      var focusedElementPtr : UnsafeMutablePointer&lt;Unmanaged&lt;AnyObject&gt;?&gt; = UnsafeMutablePointer&lt;Unmanaged&lt;AnyObject&gt;?&gt;.alloc(1)
      var error : AXError = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedApplication, focusedElementPtr)
      if let ue = focusedElementPtr.memory {
          output = ue.takeUnretainedValue() as AXUIElementRef
      }
      else {
          printError(error)
      }
      focusedElementPtr.dealloc(1)
      return output
  }
  </code></pre>
  
  <p>It may not be the best solution but now the app runs as expected :) I'm sorry if I've wasted someone's time! </p>