Re: Storing values in dictionary with their address as the key
Re: Storing values in dictionary with their address as the key
- Subject: Re: Storing values in dictionary with their address as the key
- From: Nathan Vander Wilt <email@hidden>
- Date: Tue, 29 Jul 2008 15:10:15 -0700
On Jul 29, 2008, at 8:58 AM, Nathan Vander Wilt wrote:
Right, -[NSDictionary setObject:forKey:] on a CFDictionary created
with a custom retain callback will invoke copyWithZone: before
calling your retain callback. Apple claims this is not a bug.
Getting/removing values with objectForKey:/removeObjectForKey:
should work, though; if it doesn't, I'd like to know.
Yes, accessing and removing keys should work fine. For immutable
keys (you weren't planning on changing a key behind NSDictionary's
back anyway, right) a copy is often just implemented as a retain.
Since toll-free bridged NSDictionary must provide CFDictionary
retain/release callbacks to work right on the Core Foundation side
of things, it must be copying the key, adding it to the array (where
it is retained by the callback), and then releasing it's local
reference. Take out steps one and three, which are equivalent to a
retain followed by a release, and nothing should change as far as
object accessing or removal.
Actually, that category thing is a no-go if you intend to archive your
dictionary! I repeat: beware of the code I posted thread previous!
While this is an implementation detail that could change, -
[NSDictionary initWithCoder:] calls some internal dictionary class's -
initWithObjects:forKeys:count: that also copies the keys. This leads
me to suspect that NS[Mutable]Dictionary may recopy keys in other
places at whim as well. I am not sure what happens if you try to
unarchive a CFDictionary that was archived as an NSDictionary, but
I've got better things to do than mess with a class that clearly was
not designed for what I'm trying to use it for.
If you want to use non-copied/pointer-equivalent objects as keys,
especially if there is any chance you're going to be doing any
archiving, skip NSMutableDictionary. Use NSMapTable instead, which
thankfully also supports NSCoding.
hth,
-natevw
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden