如何在OS X目标中创建CFDictionary? [英] How to create a CFDictionary in an OS X target?

查看:118
本文介绍了如何在OS X目标中创建CFDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档 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!

如何创建keysvalues参数? 到目前为止,我已经尝试过使用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不是keysvalues数组元素的正确类型:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆