4

根据文档 CFDictionaryCreate用于实例化 CFDictionary 中的swift.

func CFDictionaryCreate(_ allocator: CFAllocator!,
                  _ keys: UnsafeMutablePointer<UnsafePointer<Void>>,
                  _ values: UnsafeMutablePointer<UnsafePointer<Void>>,
                  _ numValues: CFIndex,
                  _ keyCallBacks: UnsafePointer<CFDictionaryKeyCallBacks>,
                  _ valueCallBacks: UnsafePointer<CFDictionaryValueCallBacks>) -> CFDictionary!

如何创建keysandvalues参数?到目前为止,我尝试使用 swift 的String类型,希望它能自动转换为适当的类型:

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数组元素的正确类型:keysvalues

main.swift:41:2: error: 'String' is not identical to 'UnsafePointer<Void>'
    &configKeys, &configValues, 3, &keyCallbacks, &valueCallbacks)

我怎么做UnsafePointer<Void>String

4

2 回答 2

6

对于 Swift 3,您需要额外转换为CFDictionary. 否则上下文类型 'CFDictionary' 不能与字典文字一起使用会被抛出。

let options: CFDictionary = [kSecImportExportPassphrase as String : "certificateKey"] as CFDictionary

有关更多信息,请参阅https://bugs.swift.org/browse/SR-2388

于 2016-10-08T12:35:09.207 回答
3

rintaro 的评论适用于 String 文字,但在我使用 aCFStringRef和 Swift时无效String

// Error: Contextual type 'CFDictionary' cannot be used with dictionary literal
let options: CFDictionary = [kSecImportExportPassphrase : "certificateKey"]

我不得不将 CFStringRef 转换为 Swift 字符串:

let options: CFDictionary = [kSecImportExportPassphrase as String : "certificateKey"] // works
于 2016-08-23T14:17:34.233 回答