CFDictionary 获取 Swift3 中键的值
CFDictionary get Value for Key in Swift3
我在访问 CFDictionary 中的特定(或任何)键时遇到问题。
老实说,在 Swift 中,我并没有真正理解您需要执行此操作的方式,而且我认为它过于复杂...
我的代码:
if let session = DASessionCreate(kCFAllocatorDefault) {
let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: [], options: [])!
for volume in mountedVolumes {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL) {
let diskinfo = DADiskCopyDescription(disk);
var volumeValue = CFDictionaryGetValue(diskinfo, <#T##key: UnsafeRawPointer!##UnsafeRawPointer!#>)
}
}
我想要实现的目标:将键或字段 DAVolumeNetwork 的值获取到变量 volumeValue 中。
我想我需要将 kDADiskDescriptionVolumeNetworkKey 传递给 CFDictionaryGetValue 方法?我该如何实现?
不要在 Swift 中使用 CFDictionary
。 (这是可能的,但不值得,见下文。)
CFDictionary
是免费电话
与 NSDictionary
桥接,后者又可以转换为 Swift Dictionary
.
kDADiskDescriptionVolumeNetworkKey
键的值是
CFBoolean
可以转换为 Swift Bool
.
示例:
if let session = DASessionCreate(kCFAllocatorDefault),
let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: []) {
for volume in mountedVolumes {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL),
let diskinfo = DADiskCopyDescription(disk) as? [NSString: Any] {
if let networkValue = diskinfo[kDADiskDescriptionVolumeNetworkKey] as? Bool {
print(networkValue)
}
}
}
}
为了完整起见:这是必要的指针
在 Swift 3:
中调用 CFDictionaryGetValue
if let session = DASessionCreate(kCFAllocatorDefault),
let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: []) {
for volume in mountedVolumes {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL),
let diskinfo = DADiskCopyDescription(disk) {
if let ptr = CFDictionaryGetValue(diskinfo, Unmanaged.passUnretained(kDADiskDescriptionVolumeNetworkKey).toOpaque()) {
let networkValue = Unmanaged<NSNumber>.fromOpaque(ptr).takeUnretainedValue()
print(networkValue.boolValue)
}
}
}
}
您可以将 CFDictionaryRef
转换为 Swift 字典:
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL), let diskinfo = DADiskCopyDescription(disk) as? [String: AnyObject] {
}
然后将 kDADiskDescriptionVolumeNetworkKey
转换为 Swift 字符串:
var volumeValue = diskinfo[kDADiskDescriptionVolumeNetworkKey as String]
我在访问 CFDictionary 中的特定(或任何)键时遇到问题。 老实说,在 Swift 中,我并没有真正理解您需要执行此操作的方式,而且我认为它过于复杂...
我的代码:
if let session = DASessionCreate(kCFAllocatorDefault) {
let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: [], options: [])!
for volume in mountedVolumes {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL) {
let diskinfo = DADiskCopyDescription(disk);
var volumeValue = CFDictionaryGetValue(diskinfo, <#T##key: UnsafeRawPointer!##UnsafeRawPointer!#>)
}
}
我想要实现的目标:将键或字段 DAVolumeNetwork 的值获取到变量 volumeValue 中。 我想我需要将 kDADiskDescriptionVolumeNetworkKey 传递给 CFDictionaryGetValue 方法?我该如何实现?
不要在 Swift 中使用 CFDictionary
。 (这是可能的,但不值得,见下文。)
CFDictionary
是免费电话 与NSDictionary
桥接,后者又可以转换为 SwiftDictionary
.kDADiskDescriptionVolumeNetworkKey
键的值是CFBoolean
可以转换为 SwiftBool
.
示例:
if let session = DASessionCreate(kCFAllocatorDefault),
let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: []) {
for volume in mountedVolumes {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL),
let diskinfo = DADiskCopyDescription(disk) as? [NSString: Any] {
if let networkValue = diskinfo[kDADiskDescriptionVolumeNetworkKey] as? Bool {
print(networkValue)
}
}
}
}
为了完整起见:这是必要的指针 在 Swift 3:
中调用CFDictionaryGetValue
if let session = DASessionCreate(kCFAllocatorDefault),
let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: []) {
for volume in mountedVolumes {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL),
let diskinfo = DADiskCopyDescription(disk) {
if let ptr = CFDictionaryGetValue(diskinfo, Unmanaged.passUnretained(kDADiskDescriptionVolumeNetworkKey).toOpaque()) {
let networkValue = Unmanaged<NSNumber>.fromOpaque(ptr).takeUnretainedValue()
print(networkValue.boolValue)
}
}
}
}
您可以将 CFDictionaryRef
转换为 Swift 字典:
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volume as CFURL), let diskinfo = DADiskCopyDescription(disk) as? [String: AnyObject] {
}
然后将 kDADiskDescriptionVolumeNetworkKey
转换为 Swift 字符串:
var volumeValue = diskinfo[kDADiskDescriptionVolumeNetworkKey as String]