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中正确使用CFStringGetCString?_Swift_Cfstring - Fatal编程技术网

如何在swift中正确使用CFStringGetCString?

如何在swift中正确使用CFStringGetCString?,swift,cfstring,Swift,Cfstring,我正在看文件找你 和 CFStringGetCString获取参数buffer:unsafemeutablepointer AXUIElementCopyAttributeValue获取参数值:unsafemeutablepointer 对于后者,我可以这样打电话: var value: CFTypeRef? let err = AXUIElementCopyAttributeValue(element, attribute as CFString, &value); 这满足了文档要求

我正在看文件找你 和

CFStringGetCString
获取参数
buffer:unsafemeutablepointer
AXUIElementCopyAttributeValue
获取参数
值:unsafemeutablepointer

对于后者,我可以这样打电话:

var value: CFTypeRef?
let err = AXUIElementCopyAttributeValue(element, attribute as CFString, &value);
这满足了文档要求类型为
CFTypeRef
unsafemtablepointer

然而,我不能通过这样做来应用同样的逻辑

let buffer: Int8!
CFStringGetCString(attribute as! CFString, &buffer, 2048, CFStringBuiltInEncodings.UTF8.rawValue)
我也试过了

let buffer: Int8?
CFStringGetCString(attribute as! CFString, &buffer!, 2048, CFStringBuiltInEncodings.UTF8.rawValue)
无论哪种方式,它都会抱怨在初始化之前使用了
缓冲区
,尽管它从未抱怨在具有类似参数要求的工作方法中使用

我所看到的
CFStringGetCString
的所有工作示例都使用了类似于objective-c的语法和
*
。我不知道这里的捷径是什么

我还尝试了这种方式来获得我想要的价值:

let app = AXUIElementCreateSystemWide();
var valueString = "";

var value: CFTypeRef?
// An exception on execution happens here when passing app
// Passing in the element I clicked instead of app
// yields error -25205 (attributeunsupported)
let err = AXUIElementCopyAttributeValue(app, "AXFocusedApplication" as CFString, &value);
if (err == AXError.success) {
    valueString = value! as! NSString as String;
} else {
    print("ERROR!");
    print(err.rawValue);
}

return valueString;

你为什么用
CFStringGetCString
折磨自己?如果您在Swift中有一个
CFString
,您可以将其转换为
String
,并从中获得一个C字符串:

let cString: [Int8] = (cfString as String).cString(using: .utf8)!
还要注意,
kAXFocusedApplicationAttribute
的值是而不是a
CFString
。它是一个
AXUIElement

这是我的操场测试:

import Foundation
import CoreFoundation

let axSystem = AXUIElementCreateSystemWide()
var cfValue: CFTypeRef?
AXUIElementCopyAttributeValue(axSystem, kAXFocusedApplicationAttribute as CFString, &cfValue)
if let cfValue = cfValue, CFGetTypeID(cfValue) == AXUIElementGetTypeID() {
    let axFocusedApplication = cfValue
    print(axFocusedApplication)
}
第一次执行这个游戏时,我看到一个系统对话框,告诉我需要授予Xcode权限来控制我的计算机。我进入系统首选项>安全和隐私>隐私>可访问性,在列表底部找到Xcode,并打开其复选框

以下是操场的输出:

<AXUIElement Application 0x7fb2d60001c0> {pid=30253}

这有帮助吗?@MartinR这基本上说明了如何做我所做的事情,它适用于
AXUIElementCopyAttributeValue
。对于
unsafemeutablepointer
,在
Int8
CFString
之间存在一些额外的复杂情况,我无法理解。在这种情况下,我无法真正初始化为
nil
。在Swift中应该不需要使用CFStringGetCString。您可以(有条件地)将值强制转换为
String
–你需要C字符串做什么?@MartinR I发布了我最初尝试过的解决方案,然后返回到
CFStringGetCString
,这是我在C++中看到的用法。如果让valueString=value as,你尝试过
吗?String{print(valueString)}
(如参考问答中所述)?我本想回来写一篇文章,说我了解了
kAXFocusedApplicationAttribute
常量,但你在更新问题时击败了我:)
if let frontAppName = NSWorkspace.shared.frontmostApplication?.localizedName {
    print(frontAppName)
}