CFDictionary returns null
I'm attempting to use Core Foundation for the first time and I must be missing something. I am attempting to use NSObjects as my keys and values for a CFMutableDictionaryRef.
I have the following test app:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString* key = @"testKey";
NSObject* item = [[NSObject alloc] init];
const CFDictionaryKeyCallBacks keyCB = kCFTypeDictionaryKeyCallBacks;
const CFDictio开发者_如何学运维naryValueCallBacks valCB = kCFTypeDictionaryValueCallBacks;
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &keyCB, &valCB);
CFDictionaryAddValue(dict, item, key);
NSLog(@"[CH] added = %@", CFDictionaryGetValue(dict, key));
NSLog(@"[CH] size = %ld", CFDictionaryGetCount(dict));
}
The output:
2011-07-14 16:47:40.568 TestCoreFoundation[61343:903] [CH] added = (null) 2011-07-14 16:47:40.578 TestCoreFoundation[61343:903] [CH] size = 1
You have the call to CFDictionaryAddValue backwards. It's dict, key, and then value:
CFDictionaryAddValue(dict, key, item);
Other than that, it looks fine.
Hope that helps.
精彩评论