How to access CFDictionary in Swift 3?
Be careful converting a CFDictionary
to a Swift native dictionary. The bridging is actually quite expensive as I just found out in my own code (yay for profiling!), so if it's being called quite a lot (as it was for me) this can become a big issue.
Remember that CFDictionary
is toll-free bridged with NSDictionary
. So, the fastest thing you can do looks more like this:
let cfDictionary: CFDictionary = <code that returns CFDictionary>
if let someValue = (cfDictionary as NSDictionary)["some key"] as? TargetType {
// do stuff with someValue
}
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"])
}