I'm trying to make use of Accesibility API in Swift, to register for notifications of window properties of the current frontmost app. However I couldn't find any examples/detailed info about AXObserverCreate function in swift. There is one really good answer(link is external) for objective-C, but I believe I'm facing a syntax-ish error here since I'm trying to convert it to Swift -which I am quite newbie at-.

Here is the code I'm trying:

var observer: AXObserver?
var axErr:AXError = AXObserverCreate(app.processIdentifier, windowCreatedCallback as! AXObserverCallback, &observer)

func windowCreatedCallback(observer:AXObserver, element:AXUIElement , notificationName:CFString, refcon: UnsafeMutableRawPointer) -> Void {
print("Callback Succesful")
}

So the compiler stops while reading the AXObserverCreate line and prints (11db) to console, which I think shows a problem about dereferencing a pointer (?)

Also says: "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

Almost same question was asked here(link is external), and accepted solution for swift 2.2 was to use closures, which I also tried like this and got the same error as above:

var observer: AXObserver?
let axErr = AXObserverCreate(app.processIdentifier, { (observer: AXObserver, element: AXUIElement, notificationName: CFString, refcon: UnsafeMutableRawPointer) -> Void in
print("Callback Succesful")
} as! AXObserverCallback, &observer)

Also if I don't use 'as! AXObserverCallback', build fails.

Any help would be greatly appreciated, thanks and happy coding!