RE: NSMutableDictionary <-- setter method
RE: NSMutableDictionary <-- setter method
- Subject: RE: NSMutableDictionary <-- setter method
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Wed, 28 Apr 2004 15:17:07 -0400
>
Greetings:
>
I'm trying to write a setter method for a NSMutableDictionary
>
instance variable. I understand that I can't merely do an equality
>
assignment such as:
>
>
>
- (void) setCities: (NSMutableDictionary *) theCities {
>
Cities = theCities; // Wrong.
>
}
That's not quite right. It just isn't a complete solution. If you retain
theCities and release Cities, it is quite normal to use = for assignment.
- (void) setCities:(NSMutableDictionary *)theCities
{
[retain theCities];
[release cities]; //really should be lowercase
cities = theCities;
}
And of course there are other ways of accomplishing similar things, like:
- (void) setCities:(NSMutableDictionary *)theCities
{
[cities autorelease];
cities = [theCities mutableCopy];
}
But they all use = somewhere.
You may be thinking of the fact that you often cannot use == for comparison
because objects are pointers and == tests the addresses, not the contents of
the objects.
>
>
However, I don't see a 'copy' method available to this class; didn't
>
work.
NSMutableDictionary conforms to both NSCopying and NSMutableCopying. The
methods are actually implemented by NSObject and overridden in
NSMutableDictionary, which is why you do not see the methods documented
there.
Always check protocols and superclasses.
>
>
So I discovered this:
>
>
- (void) setCities: (NSMutableDictionary *) theCities {
>
[Cities setDictionary:theCities];
>
[theCities release]; ----- ?
>
}
>
Doesn't seem like you understand memory management yet. Did this method
create theCities? No. Did this method retain theCities? No. So there's
no reason to release theCities, and in fact it's a mistake to do so.
Jonathan
_______________________________________________
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.