Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating custom CFDictionary callbacks
    text
    copied!<p>I am trying to implement categories for NSMutableDictionary class with at least two methods: one for NSDictionary that <strong>retains</strong> (not copies) its keys, and one for NSDictionary that <strong>weak references</strong> its keys (i.e. does nothing to them).</p> <p>Values are simply retained in both cases.</p> <p>Thus, as CFDictionaryRef is said to be toll-free bridged with NSDictionary, I actually do the following:</p> <pre><code>+ (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, &amp;keyCallbacks, &amp;valueCallbacks) autorelease]; } </code></pre> <p>The second method (for retained keys) looks alike and is not presented here. Scary functions inside the code are:</p> <pre><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]; }; </code></pre> <p>I had to write these myself as soon as I haven't found standard core foundation callbacks for retaining and releasing keys.</p> <p>I plan to use these categories for dictionaries that will store subclasses NSObjects only. The question is: are these valid callbacks for this case? Is there something wrong in my code besides that?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload