我正在使用LSCopyApplicationURLsForBundleIdentifier根据包标识符获取目标系统上已安装的第三方应用程序的URL。但是,当试图从返回的CFArray中检索第一个URL时,我在CFArrayGetValueAtIndex处的调试器中不断收到以下错误:
线程1:EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)
以下是我的Swift代码:

let urls = LSCopyApplicationURLsForBundleIdentifier("com.aa.bb" as CFString, nil)
if (urls != nil) {
     let url = unsafeBitCast(CFArrayGetValueAtIndex(urls as! CFArray, 0), to: CFString.self)
}
urls?.release()
url?.release()

如何正确提取URL,最好是String

最佳答案

太复杂了,使用takeRetainedValue()获取指针对象(它可以正确处理内存管理),并将其转换为[URL]

if let urls = LSCopyApplicationURLsForBundleIdentifier("com.aa.bb" as CFString, nil)?.takeRetainedValue() as? [URL],
   let url = urls.first {
     print(url)
}

unsafeBitCast(CF)URL无论如何都是不可能的,要获得字符串路径写入
print(url.path)

关于swift - 使用LSCopyApplicationURLsForBundleIdentifier和CFArrayGetValueAtIndex获取应用程序的URL/路径时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55246691/

10-09 18:32
查看更多