Collection callbacks and toll free bridging
Christiaan Hofman
cmhofman at gmail.com
Wed Apr 2 08:05:53 PDT 2008
On 2 Apr 2008, at 4:45 PM, Adam R. Maxwell wrote:
>
> On Apr 2, 2008, at 7:25 AM, Christiaan Hofman wrote:
>> Hi all,
>>
>> I have some questions about how toll free bridging handles
>> callbacks for collections. I've searched the docs but came out
>> empty. Basically I have the following 2 questions:
>>
>> 1. Are Cocoa accessors doing anything besides some checking and
>> wrapping CF functions?
>> 2. What callbacks are used by collections initialized using Cocoa?
>>
>> Let me make this a bit more explicit for the example of
>> CFMutableDictionaryRef/NSMutableDictionary. NSMutableDictionary is
>> documented to copy the keys. So a more concrete version of my
>> question is: are the keys copied by the Cocoa methods, or by the CF
>> key callbacks?
>>
>> Context: I need a dictionary for keys that do not conform to
>> NSCopying. So can I use just use CFDictionaryCreateMutable to
>> create the dictionary and then use Cocoa methods for the rest?
>
> No, NSMutableDictionary is weird about this. You must not use -
> setObject:forKey: because the copying is done in that method. Mike
> Ash pointed this out to me on cocoa-dev a couple of years ago:
>
> http://www.cocoabuilder.com/archive/message/cocoa/2006/5/13/163439
>
> My bug report on this was duped to his, and the conclusion is that
> if you use setObject:forKey:, it will create a copy of your key,
> then invoke your custom callbacks /on the copy/:
>
> http://www.cocoabuilder.com/archive/message/cocoa/2006/7/10/167259
>
> Once you've used CFDictionaryAddValue, you can safely use
> objectForKey: and other Cocoa methods, though. I still think the
> setObject:forKey: behavior is wrong, but at least I can avoid it.
>
> --
> adam
Thanks Adam. Now my question was general, and this is just a specific
example. Does anyone know the general story for any collection? Or is
the general story that I cannot trust toll free bridging for anything?
Christiaan
More information about the MacOSX-dev
mailing list