I'm trying to create a semi-complex set of view animations (think an animated version of an NSMatrix
form, where rows slide around as other rows are added or removed), and to build the animation I'm making a little helper class.
There I have to keep track of the different views, their ordered indices, and few some other values associated with their animations.
To that end, I'm using an NSArray
instance to keep track of the ordering (indices) of the views, and I'd like to use an NSDictionary
with the views as keys to keep track of the values (the values themselves are in nested dictionaries). I.e. I'd like to be able to do something this, for instance (pseudo code):
NSMutableDictionary* viewValuesDict = [NSDictionary dictionary];
// Loop thru an ordered NSArray
for( (NSView*) view in viewsArray ) {
// Get some values we'll need later
NSDictionary* associatedValues = [view getSomeValues];
// ...and put them into viewValuesDict...
[viewValuesDict setObject:associatedValues forKey:view];
// and then things break because the NSView 'view'
// doesn't support copyWithZone.... darn
}
Problem is, I of course can't use NSView
instances as dictionary keys, because the keys are added using copyWithZone
, which NSView
does not implement.
So, what's a good way to get a unique key for an NSView
instance? I could conceivably use [obj description]
since the memory address you get back is a perfect UID, but of course the system has to work with any kind of NSView
subclass that might return something else entirely, so that's no good.
Or should I maybe try something else entirely? Is there maybe some alternative to NSDictionary
, where keys just aren't copied? Because in this case I really have no need for the keys to ever be copied.
Sometimes there are occasions where you want to use a view (whether NS
or UI
) as the key in a dictionary. I've come across one such situation. I would've preferred to use objc_setAssociatedObject
, but that requires Snow Leopard. Boxing with NSValue
will work, but if you need to be doing lots and lots of look ups given a view, the continual boxing and unboxing of pointers may become tedious.
There are two options to creating an NSView => <object>
dictionary.
NSMapTable
CFMutableDictionaryRef
NSMapTable
is a class introduced in 10.5 that is very similar to an NSMutableDictionary
, except that it has extra abilities that make it work more nicely with garbage collection. In your case, you'll probably want a map table with "weak" keys and "strong" values, but read the documentation for all the fun details.
CFMutableDictionaryRef
is the Core Foundation equivalent of an NSDictionary
(they are toll-free bridged), but it has some extra creation options. You create one using CFDictionaryCreateMutable()
, and that wants two struct
parameters. One is a structure that defines the memory management (and other) behavior of how to deal with the keys of the dictionary, and the other is a struct
for defining the behavior of the values. You can create a CFMutableDictionaryRef
with the options of retaining the keys (instead of copying them) and then retaining the values. Once you've done this, you can cast the CFMutableDictionaryRef
to an NSMutableDictionary
and use it as you'd expect, just that the keys will be retained instead of copied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With