Re: NSMutableDictionary <-- setter method
Re: NSMutableDictionary <-- setter method
- Subject: Re: NSMutableDictionary <-- setter method
- From: Nick Zitzmann <email@hidden>
- Date: Wed, 28 Apr 2004 13:10:21 -0600
On Apr 28, 2004, at 11:53 AM, Frederick C. Lee wrote:
However, I don't see a 'copy' method available to this class; didn't
work.
If the object complies with the NSCopying protocol, then -copy will
work. NSMutableDictionary complies with the protocol. However, using
-copy to copy an NSMutableDictionary will make an immutable copy, so
don't use that if "Cities" is supposed to be mutable. In that case,
you'd use -mutableCopy instead, which is part of the NSMutableCopying
protocol.
So I discovered this:
- (void) setCities: (NSMutableDictionary *) theCities {
[Cities setDictionary:theCities];
[theCities release]; ----- ?
}
Is this the correct approach? Should I release 'theCities'?
No, you should never do that. theCities could be in the autorelease
pool, and if the method releases it, then your application will crash.
Instead, you need to do something like this: (written in Mail)
- (void)setCities:(NSMutableDictionary *)theCities
{
if (Cities != theCities) // make sure these don't point to the same
object
{
[Cities release]; // get rid of the existing object
Cities = [theCities retain]; // and hold onto the new one
}
}
Nick Zitzmann
<
http://www.chronosnet.com/>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.