views:

38

answers:

1

I am trying to manage a few animations in my scene using the following method:

  1. In a loop, create animated UIImageView, set the on stopped method. Also, store this UIImageView in a NSMutableDictionary. The animation ID is the same as the key in the dictionary.

  2. When this animation is stopped, get the animation ID. Use [animations objectForKey: animationID] to get the imageview object. I remove the imageview from the superview and release it. I also remove the object from the dictionary using the key.

  3. Create another animated object, rinse, repeat.

The problem is, when I go to get the object based on the key, NULL is returned. I assume this is related to how the dictionary works-- it copies the values instead of retaining them. Because it is copied, I can't actually remove that object.

How can I get NSDictionary to NOT copy these objects (so I have the original) or how do I release these animated objects and remove them from the superview when they are done animating?

+1  A: 

NSDictionary doesn't copy objects (only keys). The only reason you'd be getting NULL back is if you supply the wrong key, or the object has already been removed.

It isn't 100% clear from your description, but you seem to suggest that you "2. ... remove the object from the dictionary ..." and then later "... got to get the object ...". If these are two separate activities, then you most certainly won't find the object there on the second visit.

Marcelo Cantos
Hmm, maybe I misread the other stack overflow article. Let me check my code again...
Andrew M
Looks like it is null even right after I set it. Will investigate further. Thanks.
Andrew M
As a followup, I never called [[NSMutableDictionary alloc] init]. :(
Andrew M
It annoys me no end that messages to `nil` fail silently; this catches me out all the time (I even forgot to consider this possibility in your case). Well, I'm glad to see you found the problem eventually.
Marcelo Cantos