Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating custom CFDictionary callbacks
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COa bit OT: you should generally prefix your category methods of NSObject subclasses which have been provided by another developer (such as apple). for example: `+ (NSMutableDictionary *)kbws_dictionaryWithWeakReferencedKeysForCapacity: (NSUInteger)capacity`. also, you'd generally want to provide the hash callback as well. hash codes are very important for cfdictionaries - providing this will be best in most cases because it gives the typical (expected) result. if you provide no hash callback, hash codes are based on address (which is valid behaviour in some cases).
      singulars
    2. COthanks for the comment on the topic, Justin; hash codes, yeah, I'm warned about that and will deal a bit later with them :) could you just explain the reason for prefixing method names?
      singulars
    3. COyw. it's the same as the reason for prefixing classnames. objc uses a flat namespace for classes and methods. there's also no means to specify a category method via syntax. therefore, if any binary you load into your app uses the same class or method name, then one of the implementations will be ignored, and somebody won't get the result/effect/implementation they expect from messaging. prefixing reduces the likelihood of this. which method is ignored is undefined (afaik), but you'll probably end up with the first implementation which was loaded into the process.
      singulars
 

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