将字典键传递给 CFDictionaryGetValue
pass dictionary key to CFDictionaryGetValue
由于 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")))
然而returns这是一个错误。我不知道如何将字典键传递给此方法。如果我删除 UnsafePointer("HTTPPort")
并改用 "HTTPPort"
,我会收到运行时错误。"
如何访问字典值?
最简单的解决方案是利用免费桥接
在 CFDictionary
和 NSDictionary
之间,并使用 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)
}
}
由于 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")))
然而returns这是一个错误。我不知道如何将字典键传递给此方法。如果我删除 UnsafePointer("HTTPPort")
并改用 "HTTPPort"
,我会收到运行时错误。"
如何访问字典值?
最简单的解决方案是利用免费桥接
在 CFDictionary
和 NSDictionary
之间,并使用 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)
}
}