How to create CFDictionary in target OS X?

According to the documentation, CFDictionaryCreate used to instantiate a CFDictionary in swift .

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

How to create keys and values arguments? So far, I have been trying to use swift String , hoping that it will be automatically converted to the 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) 

Unfortunately, I get an error: String not the correct type for the elements of the keys and values array:

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

How do I make UnsafePointer<Void> from String ?

+3
source share
2 answers

For Swift 3, you need extra casting to CFDictionary . Otherwise, the context type "CFDictionary" cannot be used with a dictionary literal.

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

See https://bugs.swift.org/browse/SR-2388 for more details.

+4
source

The rintaro comment worked for string literals, but not when I worked with CFStringRef and Swift String :

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

I needed to draw a CFStringRef as a Swift String:

 let options: CFDictionary = [kSecImportExportPassphrase as String : "certificateKey"] // works 
+2
source

Source: https://habr.com/ru/post/1257988/


All Articles