1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| const void* RetainCallback(CFAllocatorRef allocator, const void* value) { return CFRetain(value); }
void ReleaseCallback(CFAllocatorRef allocator, const void* value) { CFRelease(value); }
CFDictionaryKeyCallBacks keyCallbacks = { 0, RetainCallback, ReleaseCallback, NULL, //copyDescription取值为NULL,采用默认实现 //equal和hash发布采用CFEqual和CFHash这二者采用的做法与NSMutableDictionary的默认实现相同 CFEqual, // CFEqual最终会调用isEqual方法 CFHash, };
CFDictionaryValueCallBacks valueCallback = { 0, RetainCallback, ReleaseCallback, NULL, CFEqual, }; // 用CFMutableDictionaryRef来创建NSMutableDictionary - (void)createMutableDictionaryForCFMutableDictionaryRef { CFMutableDictionaryRef aCFDicionary = CFDictionaryCreateMutable(NULL, 0, &keyCallbacks, &valueCallback); NSMutableDictionary *anNSDictionary = (__bridge_transfer NSMutableDictionary *)aCFDicionary; anNSDictionary[@"a"] = @123; [anNSDictionary removeObjectForKey:@"a"]; }
|