Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中访问Objective-C指针_Objective C_Swift_Pointers - Fatal编程技术网

在Swift中访问Objective-C指针

在Swift中访问Objective-C指针,objective-c,swift,pointers,Objective C,Swift,Pointers,我有这个Objective-C代码片段,我想用Swift来表达 CFArrayRef windowList; AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute, (CFTypeRef *)&windowList); if ((!windowList) || CFArrayGetCount(windowList)<1) continue; AXUIElementRef windowRef = (AX

我有这个Objective-C代码片段,我想用Swift来表达

CFArrayRef windowList;
AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute, (CFTypeRef *)&windowList);

if ((!windowList) || CFArrayGetCount(windowList)<1)
        continue;

AXUIElementRef windowRef = (AXUIElementRef) CFArrayGetValueAtIndex( windowList, 0);
CFTypeRef role;
AXUIElementCopyAttributeValue(windowRef, kAXRoleAttribute, (CFTypeRef *)&role);         
CFArrayRef窗口列表;
AXUIElementCopyAttributeValue(appRef、kAXWindowsAttribute、(CFTypeRef*)和windowList);

如果((.WOLDOWLIST))CFARAYGETCOUNT(Windows列表)< P> CF数组是NSCORE的基础C版本(因为C不理解目标C NSObject).Swift文件同时覆盖NSArray和CFArray,因此您不需要使用指针;如果您将
不可分配指针作为最后一个指针传递,您应该能够将其转换为具有
as?
的适当类型的Swift数组
AXUIElementCopyAttributeValue()
的参数,则必须 通过分配(并最终释放)内存对其进行初始化:

同样地:

if let window = windowList.first {
    var value: AnyObject?
    let result = AXUIElementCopyAttributeValue(window, kAXRoleAttribute as CFString, &value)
    if result == .success, let role = value as? String {
        // use `role` ...
    }
}

可以定义一个封装 所有演员:

func axUICopyAttributeValue<T>(of element: AXUIElement, attribute: String, as type: T.Type) -> T? {
    var value: AnyObject?
    let result = AXUIElementCopyAttributeValue(element, attribute as CFString, &value)
    if result == .success, let typedValue = value as? T {
        return typedValue
    }
    return nil
}

这有帮助吗?@MartinR TNT,如示例
var windowList:CFArray?
AXUIElementCopyAttributeValue(appRef,kAXWindowsAttribute作为CFString,&windowList)
也不编译:无法传递“CFTypeRef”类型的不可变值(也称为“可选”)正如我所说,在“AXUIElementCopyAttributeValue(appRef,kAXWindowsAttribute作为CFString,windowList)”中,您将使用什么Swift类型的windowList谢谢!有两个问题,1)我必须强制转换到
[AXUIElement]
2)
var值:AnyObject,在哪里可以找到该声明是否为一个对象分配了任何内存,或者只是对任何对象的引用(或零)1)声明这些值是“可访问性对象的数组”–2) 可选变量隐式初始化为
nil
。没有分配内存。现在编译正常,但返回
AXError
。在本文中获得了其他参数:AXError的行值为kAXErrorCannotComplete=-25204,Doku说:发生了一个基本错误,例如在处理过程中分配内存失败。有没有可能,我必须为
var-value:AnyObject分配任何东西?
?对不起,我不知道。我通过引用当前的应用程序
让appRef=AXUIElementCreateApplication(getpid())
对它进行了测试,结果成功了。
var value: AnyObject?
let result = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &value)
if result == .success, let windowList = value as? [AXUIElement] {
    // use `windowList`
}
if let window = windowList.first {
    var value: AnyObject?
    let result = AXUIElementCopyAttributeValue(window, kAXRoleAttribute as CFString, &value)
    if result == .success, let role = value as? String {
        // use `role` ...
    }
}
func axUICopyAttributeValue<T>(of element: AXUIElement, attribute: String, as type: T.Type) -> T? {
    var value: AnyObject?
    let result = AXUIElementCopyAttributeValue(element, attribute as CFString, &value)
    if result == .success, let typedValue = value as? T {
        return typedValue
    }
    return nil
}
if let windowList = axUICopyAttributeValue(of: appRef, attribute: kAXWindowsAttribute, as:[AXUIElement].self) {

    for window in windowList {
        if let role = axUICopyAttributeValue(of: window, attribute: kAXRoleAttribute, as: String.self) {

            // ...
        }
    }
}