• 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: Ambroise Confetti <email@hidden>
  • Date: Sun, 23 Nov 2003 18:41:26 +0100

Le 23 nov. 03, ` 09:55, Graeme Mathieson a icrit :

> That brings up another thing I'm not entirely sure about. Surely
> somebody could in fact pass in an NSMutableString and, despite the
> fact my object thinks its immutable, merrily mess around with my
> object's data without going through the appropriate accessors? I
> guess the only way to be sure would be something along the lines of:
>
> - (id)initWithString:(NSString *)aString
> {
> if((self = [super init])) {
> if([aString isMemberOfClass:[NSString class]]) {
> baz = [aString retain];
> } else {
> baz = [aString copy];
> }
> }
> return self;
> }

Quoting the Cocoa docs (Memory Management / Implementing Object Copy):

"Copying Mutable Versus Immutable Objects

Where the concept immutable vs. mutable applies to an object,
NSCopying produces immutable copies whether the original is immutable
or not. Immutable classes can implement NSCopying very efficiently.
Since immutable objects dont change, there is no need to duplicate
them. Instead, NSCopying can be implemented to retain the original. For
example, copyWithZone: for an immutable string class can be implemented
in the following way.
- (id)copyWithZone:(NSZone *)zone {
return [self retain];
}"

So I guess that when you call copy on an immutable NSString, all you
get is the same object with a higher retain count.

Said differently: you don't have to take care about choosing between
retaining or copying an object (provided all you want is something that
behaves like a copy), since its implementation will choose the best
method (assuming the developer did a good job -- anyway, that's an
assumption you have to make everytime you're writing code in a
higher-level language than assembler...).

(I didn't say copy should always be used instead of retain! Anyway, all
these topics are covered with understandable and pertinent answers in
the Cocoa docs, topic "Memory Management".)


Ambroise

http://www.cellulo.info/
ICQ 4508259
AIM atvaark

[demime 0.98b removed an attachment of type text/directory which had a name of Ambroise Confetti.vcf]
_______________________________________________
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.

  • Follow-Ups:
    • Re: dealloc and instance variables
      • From: Jonathan Jackel <email@hidden>
References: 
 >Re: dealloc and instance variables (From: Jörn Salewski <email@hidden>)
 >Re: dealloc and instance variables (From: Graeme Mathieson <email@hidden>)

  • Prev by Date: Re: OT: Meaning of .m extension
  • Next by Date: Re: Controller Binding
  • Previous by thread: Re: dealloc and instance variables
  • Next by thread: Re: dealloc and instance variables
  • Index(es):
    • Date
    • Thread