Creating Custom CFDictionary Callbacks

I am trying to implement categories for the NSMutableDictionary class using at least two methods: one for NSDictionary, which stores (and not copies) of its keys, and one for NSDictionary, which weak links its keys (i.e. do nothing for them).

Values ​​are simply stored in both cases.

So, since CFDictionaryRef is called duty-free bridge with NSDictionary, I actually do the following:

+ (NSMutableDictionary *)dictionaryWithWeakReferencedKeysForCapacity: (NSUInteger)capacity
{   
    CFDictionaryKeyCallBacks    keyCallbacks    = { 0, NULL, NULL, CFCopyDescription, CFEqual, NULL }; 
    CFDictionaryValueCallBacks  valueCallbacks  = { 0, ___f_KBWS_DICTIONARY_RETAIN_CALLBACK, ___f_KBWS_DICTIONARY_RELEASE_CALLBACK, CFCopyDescription, CFEqual };

    return [(id)CFDictionaryCreateMutable(NULL, capacity, &keyCallbacks, &valueCallbacks) autorelease]; 
}

The second method (for stored keys) looks the same and is not presented here. Scary functions inside the code:

static const void *___f_KBWS_DICTIONARY_RETAIN_CALLBACK(CFAllocatorRef allocator, const void *value)
{
    id object = (id)value;
    return [object retain];
};

static void ___f_KBWS_DICTIONARY_RELEASE_CALLBACK(CFAllocatorRef allocator, const void *value)
{
    id object = (id)value;
    return [object release];
};

I had to write them myself as soon as I did not find the standard core callbacks for saving and releasing keys.

, NSObjects. : ? - , ?

+3
2

, . , , "" NSMutableDictionaries "", NSMutableDictionaries.

, - NSMutableDictionary , . CFMutableDictionary, Toll-Free Bridging, .

, , , , . , " " "NSMutableDictionary", .

, NSMutableDictionary, "MyWeakRefKeyMutableDictionary", . , , - NSMutableDictionary, , .

+2

, .

, CFRetain CFRelease. , kCFTypeDictionaryValueCallBacks.

+ (NSMutableDictionary *)dictionaryWithWeakReferencedKeysForCapacity: (NSUInteger)capacity
{   
    CFDictionaryKeyCallBacks    keyCallbacks    = { 0, NULL, NULL, CFCopyDescription, CFEqual, NULL }; 

    return [(id)CFDictionaryCreateMutable(NULL, capacity, &keyCallbacks, &kCFTypeDictionaryValueCallBacks) autorelease]; 
}
+2

Source: https://habr.com/ru/post/1788725/


All Articles