• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: dealloc and instance variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Re: dealloc and instance variables (From: Jörn Salewski <email@hidden>)
 >Re: dealloc and instance variables (From: Graeme Mathieson <email@hidden>)
 >Re: dealloc and instance variables (From: Ambroise Confetti <email@hidden>)

  • Prev by Date: Re: Search icon (magnifying glass) in search field
  • Next by Date: Re: NSArrayController array-observation
  • Previous by thread: Re: dealloc and instance variables
  • Next by thread: Re: dealloc and instance variables
  • Index(es):
    • Date
    • Thread