Obj-C AXObserverCallback 可以转换为 Swift 吗? [英] Can an Obj-C AXObserverCallback be converted to Swift?

查看:40
本文介绍了Obj-C AXObserverCallback 可以转换为 Swift 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AXObserverCallback 可以转换为 Swift 吗?我遇到的问题似乎与 void* 有关,我知道它在 Swift 中不是有效类型.

Can an AXObserverCallback be converted to Swift? The problem I am having seems to be with the void* which I know is not a valid type in Swift.

我正在尝试将 Obj-C 代码转换为 Swift,但无法编译代码.我不确定 AXObserverCallback 可以转换也可以不转换.Objective-C 声明是:

I am attempting to convert Obj-C code to Swift and I am unable to get the code to compile. I am uncertain if the AXObserverCallback can be converted or not. The Objective-C declaration is:

typedef void (*AXObserverCallback)( AXObserverRef observer, AXUIElementRef element, CFStringRef notification, void *refcon);

AXObserverCallback 没有 Swift 声明.设置 AXObserver 的另一个相关函数是 AXObserverCreate,对于 Obj-C 和 Swift 都有声明.

There is no Swift declaration for AXObserverCallback. The other relevant function for setting up an AXObserver is AXObserverCreate, for which there are declarations for both Obj-C and Swift.

工作 Obj-C 代码的简化版本是:

A simplified version of the working Obj-C code is:

- (AXError)installWindowCreatedAXObserver { 
    pid_t pid = 0;
    AXObserverRef observer = NULL; 
    AXError axErr = AXObserverCreate(pid, windowCreatedCallback, &observer); 
    return axErr; 
}

void windowCreatedCallback(AXObserverRef observer, AXUIElementRef element, CFStringRef notificationName, void *refCon) { 
    NSLog(@"windowCreatedCallback"); 
} 

我尝试转换为 Swift 是:

My attempt to convert to Swift is:

func installWindowCreatedAXObserver() -> AXError { 
    let pid:pid_t = 0; 
    var observer: AXObserverRef; 
    var axErr:AXError = AXObserverCreate(application:pid, callback:windowCreatedCallback, outObserver:&observer); 
    return axErr; 
} 

func windowCreatedCallback(observer:AXObserverRef, element:AXUIElementRef, notificationName:CFStringRef, refcon:void*) -> Void { 
    println("windowCreatedCallback"); 
} 

上述 Swift 代码的初始错误位于 windowCreatedCallback 并显示预期的 ',' 分隔符".我知道这个错误是因为 void* 不是 Swift 中的类型.

The initial error with the above Swift code is at windowCreatedCallback and says "Expected ',' separator". I know this error is due to the void* which is not a type in Swift.

我尝试将 void* 更改为:voidPtr、UnsafePointer、UnsafeMutablePointer 和 AnyObject.这些都没有纠正错误.当我使用除 void* 以外的任何内容时,错误将移至 AXObserverCreate 调用,因为无法将表达式的类型转换为 AXError 类型".

I have tried changing the void* to: voidPtr, UnsafePointer<AnyObject>, UnsafeMutablePointer<Void>, and AnyObject. None of these correct the error. When I use anything except void*, then the error moves to the AXObserverCreate call as "Cannot convert the expression's type to type AXError".

所以,虽然我意识到我不能使用 void*,但回调的声明仅在 Obj-C 中并且它使用了 void*.AXObserverCallback 没有 Swift 声明,但 AXObserverCreate 却有,这似乎很奇怪.

So, although I realize I cannot use void*, the declaration for the callback is only in Obj-C and it uses a void*. It seems odd that there is not a Swift declaration for AXObserverCallback yet there is for AXObserverCreate.

我已经研究过这个问题,但一直无法解决问题.我知道我尝试使用的 Swift 回调没有声明,但是当我尝试使用 Obj-C 声明格式时,这不起作用,因为 AXObserverCreate 没有看到回调函数.

I have researched the issue and have been unable to resolve the problem. I understand that the Swift callback I am attempting to use does not have a declaration, but when I try to use the Obj-C declaration format, that does not work because AXObserverCreate does not see the callback function.

任何帮助将不胜感激!

推荐答案

似乎闭包有效(使用 Swift 2.2 测试):

It seems that closures work (tested with Swift 2.2):

let axErr = AXObserverCreate(pid, { (observer: AXObserver, element: AXUIElement, notificationName: CFString, refcon: UnsafeMutablePointer<Void>) -> Void in
    print(notificationName)
}, &observer)

这篇关于Obj-C AXObserverCallback 可以转换为 Swift 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆