How to access CFDictionary in Swift 3?

Trevor Alyn picture Trevor Alyn · Sep 17, 2016 · Viewed 8.6k times · Source

I need to read and write some data from CFDictionary instances (to read and update EXIF data in photos). For the life of me, I cannot figure out how to do this in Swift 3. The signature for the call I want is:

func CFDictionaryGetValue(CFDictionary!, UnsafeRawPointer!)

How the heck do I convert my key (a string) to an UnsafeRawPointer so I can pass it to this call?

Answer

Code Different picture Code Different · Sep 17, 2016

If you don't have to deal with other Core Foundation functions expecting an CFDictionary, you can simplify it by converting to Swift native Dictionary:

if let dict = cfDict as? [String: AnyObject] {
    print(dict["key"])
}