Collection callbacks and toll free bridging

Adam R. Maxwell amaxwell at mac.com
Wed Apr 2 07:45:07 PDT 2008


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


More information about the MacOSX-dev mailing list