gpt4 book ai didi

ios - 将字典键传递给 CFDictionaryGetValue

转载 作者:行者123 更新时间:2023-11-29 01:07:27 26 4
gpt4 key购买 nike

由于 Swift 中的 UnsafePointer,我感到头疼。

这是我要调用的方法:

func CFDictionaryGetValue(theDict: CFDictionary!, _ key: UnsafePointer<Void>) -> UnsafePointer<Void>

我就是这样做的。

 let ds: SCDynamicStoreRef = SCDynamicStoreCreate(nil, "setNet" as CFString, nil, nil)!


let list = SCDynamicStoreCopyProxies(ds)

print(list!)
print(CFDictionaryGetValue(list, UnsafePointer("HTTPPort")))

但是这会返回一个错误。我不知道如何将字典键传递给这个方法。如果我删除 UnsafePointer("HTTPPort") 并使用 "HTTPPort" 代替,我会收到运行时错误。"

如何访问字典值?

最佳答案

最简单的解决方案是利用免费桥接在 CFDictionaryNSDictionary 之间,并使用 NSDictionary访问器方法:

let ds = SCDynamicStoreCreate(nil, "setNet", nil, nil)!

if let list = SCDynamicStoreCopyProxies(ds) as NSDictionary? {
if let port = list[kSCPropNetProxiesHTTPPort as NSString] as? Int {
print("HTTPPort:", port)
}
}

但只是为了完整起见:可以CFDictionaryGetValue:

if let list = SCDynamicStoreCopyProxies(ds)  {
let key = kSCPropNetProxiesHTTPPort
let port = unsafeBitCast(CFDictionaryGetValue(list, unsafeAddressOf(key)), NSObject!.self)
if port != nil {
print("HTTPPort:", port)
}
}

关于ios - 将字典键传递给 CFDictionaryGetValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com