Re: dealloc and instance variables
Re: dealloc and instance variables
- Subject: Re: dealloc and instance variables
- From: Jonathan Jackel <email@hidden>
- Date: Sun, 23 Nov 2003 15:19:53 -0500
- (id)initWithString:(NSString *)aString
{
if((self = [super init])) {
if([aString isMemberOfClass:[NSString class]]) {
baz = [aString retain];
} else {
baz = [aString copy];
}
}
return self;
}
Don't do this. Use your accessors in your init method.
With a string setter, copy seems better than retain to me. If aString
is immutable, it is probably just retained. If it is mutable, you get
your own immutable copy and you make sure that your iVar isn't changed
without your knowledge.
The one situation where you need to be careful is if there is any
chance that the setter will take an object that does not conform to the
NSCopying protocol -- copy will cause a "does not respond to selector"
exception and the object will be neither copied nor retained. Custom
model objects and views are two common examples. If your setter might
get non-copying object, either check for conformity with the protocol
and use retain if you can't copy, or just use retain (and risk mutable
objects getting changed).
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.