Crazor Crazor - 15 days ago 7
Swift Question

How to handle UnsafePointer<Unmanaged<CFArray>?>

How do I call the following function:

func AXUIElementCopyAttributeNames(element: AXUIElement!, names: UnsafePointer<Unmanaged<CFArray>?>) -> AXError


I have a AXUIElement(Ref) (they are typealias'ed) and I'd like to call the function to get the accessibility attribute names available for that element.

Answer

I am a little bit guessing (because I have no experience with the Accessibility functions), but from the function declaration it should work like this:

let element: AXUIElementRef = ...

var ptr : Unmanaged<CFArray>? = nil
let error = AXUIElementCopyAttributeNames(element, &ptr)
if error == AXError(kAXErrorSuccess) {
    let names = ptr!.takeRetainedValue() // gives a CFArray
    // ...
}

Update for Swift 3 (untested):

var cfArray: CFArray?
let error = AXUIElementCopyAttributeNames(element, &cfArray)
if error == .success, let names = cfArray as? [String] {
    // names is [String] array ...
}