NSMutableDictionary, NSNumber and more memory management
NSMutableDictionary, NSNumber and more memory management
- Subject: NSMutableDictionary, NSNumber and more memory management
- From: Memo Akten <email@hidden>
- Date: Thu, 28 Aug 2008 12:46:08 +0100
Hi All, I have a few questions regarding using NSMutableDictionary,
NSNumbers, and more memory management.
I have a mutable dictionary, which I would like to store a bunch of
values, which my app will modify and save and load as a plist. This is
inside a QCPlugIn and it seems the autorelease pool is released after
every frame (i had an earlier post regarding this) so can't rely on
using convenience methods across frames unless I retain them
this is my init/dealloc code:
INIT:
myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:[NSNumber numberWithUnsignedInteger:0] forKey:KEY1];
[myDict setObject:[NSNumber numberWithUnsignedInteger:0] forKey:KEY2];
[myDict setObject:[NSNumber numberWithUnsignedInteger:0] forKey:KEY3];
[myDict setObject:[NSNumber numberWithUnsignedInteger:0] forKey:KEY4];
[myDict setObject:[NSNumber numberWithUnsignedInteger:0] forKey:KEY5];
DEALLOC:
[myDict release];
q1:
Is this all correct? When you setObject, the docs say that the object
receives a retain, so that means its safe (and correct) for me to do
the above? Do I need to release the NSNumbers in dealloc? or does the
NSMutableDictionary release them? I think its the latter but just
wanted to double check.
q2:
this is my updating code:
- (void) updateValue:(NSString*)key {
id oldValue = [myDict objectForKey:key];
id newValue = [NSNumber numberWithUnsignedInteger:([oldValue
unsignedIntegerValue] + 1)];
[myDict setObject:newValue forKey:key];
[oldValue release]; // do I need to release this?
}
I 'm not very experienced with ObjC, but I"m loving it so far... but
this seems a bit overkill - destroy and recreate a new NSNumber and re-
add it to the dictionary - but I couldn't get it to work any other
way. Performance isn't really an issue as this will be called at most
a couple times a second, usually a lot more infrequently - but is this
the best way to do it? I thought of having a bunch of int variables,
and update them, and reconstruct the NSDictionary every time one of
them changes, but that seemed a bit overkill too..
cheers,
_______________________________________________
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