如何在OS X目标中创建CFDictionary? [英] How to create a CFDictionary in an OS X target?
问题描述
根据文档 CFDictionaryCreate
用于实例化swift
中的CFDictionary.
According to documentation CFDictionaryCreate
is used to instantiate CFDictionary in swift
.
func CFDictionaryCreate(_ allocator: CFAllocator!,
_ keys: UnsafeMutablePointer<UnsafePointer<Void>>,
_ values: UnsafeMutablePointer<UnsafePointer<Void>>,
_ numValues: CFIndex,
_ keyCallBacks: UnsafePointer<CFDictionaryKeyCallBacks>,
_ valueCallBacks: UnsafePointer<CFDictionaryValueCallBacks>) -> CFDictionary!
如何创建keys
和values
参数?
到目前为止,我已经尝试过使用swift的String
类型,希望将其自动转换为适当的类型:
How can I create the keys
and values
arguments?
So far I've tried to use swift's String
type hoping it would be automatically converted to appropriate types:
import Foundation
var keys : [String] = ["key1", "key2"]
var values : [String] = ["value1", "value2"]
var keyCallbacks = kCFTypeDictionaryKeyCallBacks
var valueCallbacks = kCFTypeDictionaryValueCallBacks
var dict : CFDictionary = CFDictionaryCreate(kCFAllocatorDefault,
&keys, &values, 2, &keyCallbacks, &valueCallbacks)
不幸的是,我收到一条错误消息,指出String
不是keys
和values
数组元素的正确类型:
Unfortunately I receive an error saying String
is not the right type for keys
and values
array elements:
main.swift:41:2: error: 'String' is not identical to 'UnsafePointer<Void>'
&configKeys, &configValues, 3, &keyCallbacks, &valueCallbacks)
如何从String
制作UnsafePointer<Void>
?
推荐答案
对于Swift 3,您需要对CFDictionary
进行额外的转换.
否则,上下文类型'CFDictionary'不能与字典一起使用文字会被抛出.
For Swift 3 you need extra casting to CFDictionary
.
Otherwise Contextual type 'CFDictionary' cannot be used with dictionary literal is thrown.
let options: CFDictionary = [kSecImportExportPassphrase as String : "certificateKey"] as CFDictionary
请参见 https://bugs.swift.org/browse/SR-2388 更多.
这篇关于如何在OS X目标中创建CFDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!