I'm struggling to find any explanation for why CFDictionary methods return consts. Can anyone explain this to me?
I'm struggling to find any explanation for why CFDictionary methods return consts. Can anyone explain this to me?
Initialization discards qualifiers from pointer target type
id const someValue = CFDictionarySetValue(self.mappingConfigByMediatedObjectClass, mediatedObjectClass, mediatorMapping);
Sorry. Was very early in the morning.
I am using CFMutableDictionary because I need to use keys that are not copied. I am programming in cocoa touch so this is unfortunately my only option as I have no access to NSHashTable.
The correct code should have been:
id<SomeProtocol> someObject = CFDictionaryGetValue(self.someDictionary, someObjectAsKey);
So if I set someObject as a const I lose the warning.
But I want to understand why this is necessary. Why must I declare a local const variable?
The assignment to the local variable gave you a warning; exactly for that reason. To remove the warning, you have to explicitly cast the result of CFDictionaryGetValue. That basically tells the compiler "Shut up, I know what I'm doing, if it goes wrong it's all my fault".
id<SomeProtocol> someObject = (id<SomeProtocol>)CFDictionaryGetValue(self.someDictionary, someObjectAsKey);
Perhaps you should consider a lighter key.The objects I am using as keys are too heavy to be copied and I cannot guarantee they will support NSCopying anyway.
I'm certainly not enjoying my time with CFDictionary so farHowever, as far as I understand it NSDictionary/NSMutableDictionary will always copy keys. With the CFDictionary I can specify whether I want the keys to be copied when I create it, but with NSDictionary I have no choice.