Reputation: 43370
I have a requirement for a dictionary with keys that are not copied. This has lead me on a merry dance and I've ended up at the door of CFMutableDictionary.
I am trying to understand the extent to which they are interchangeable. In Apple's docs for CFMutableDictionary they state:
in a method where you see an NSMutableDictionary * parameter, you can pass in a CFMutableDictionaryRef, and in a function where you see a CFMutableDictionaryRef parameter, you can pass in an NSMutableDictionary instance.
But I wondered whether it would be possible to cast a CFMutableDictionary to NSMutableDictionary and call NSMutableDictionary's methods on it, and it seems in some cases you can;
If I create a CFMutableDictionary using CFDictionaryCreateMutable() and cast it to an NSMutableDictionary.
I can call:
[cfDictionaryCastToNSDictionary objectForKey:someKey]
I can also call:
[cfDictionaryCastToNSDictionary setObject:someObject forKey:someKey]
... which will copy the key or raise an exception if it doesn't implement NSCopying. I can also iterate through its values using a for in loop.
However if I call:
[cfDictionaryCastToNSDictionary count]
I get an exception.
My question is what exactly is going on here under the hood? At no point does the Apple documentation mention being able to call some methods that do not exist on CFMutableDictionary by casting to NSDictionary.
Upvotes: 3
Views: 2508
Reputation: 36143
If you are looking to understand problems using toll-free bridging, you should start with Mike Ash's "Toll-Free Bridging Internals" and go from there.
If you just want to get back to work, the answer is, don't do that; avoid the problem entirely by using the CF functions with the CFDictionary rather than toll-free bridging. The CFDictionary API should be enough to do whatever you're trying to do if the dictionary is being used purely internally.
Upvotes: 3