I understand that any init… method initializes a new object and that NSString stringWithString makes a copy of the parameter string as a new object. I also understand that being the objects’ owner, I can control the release/deallocation of any objects that I allocate. What I don’t understand is when would I use the stringWithString method since any local variable assigned that way would have it’s memory ‘owned’ by NSString instead of the local class.
The ‘Programming in Objective C’ book by Kochan (1st ed) uses the following code (see pages 342-344) to explain that the initWithString is preferable to stringWithString because the AddressCard class would own the name variable contents. Also, I don’t get any errors making repeated calls to the setName version with the stringWithString method. TIA!!
//header file has appropriate declarations but not included here: #import 'AddressCard.h' @implementation AddressCard; -(NSString *) name { return name; } //Recommended code: -(void) setName: (NSString *) theName { [name release] name = [[NSString alloc] initWthString: theName]; } //Incorrect code according to Kochan: -(void) setName: (NSString *) theName { [name release] name = [NSString stringWthString: theName]; } //rest of class implementation code snipped @end
That’s not correct.
Object ownership is determined by five rules, based on the name of the method you get an object from:
alloc,copy,copyWithZone, ornewhas a retain count of 1.retainincreases the receiving object’s retain count.releasedecreases the receiving object’s retain count.autoreleasetells the current autorelease pool to send the receiving object thereleasemessage “later”.stringWithString:) returns an object that it has autoreleased on your behalf.Or, digested a bit:
copy,alloc,retain, ornewreturns an object that you own.The incorrect implementation of
setName:that you show is incorrect because it stores an autoreleased object in an instance variable, when you mean to own the object. You should retain it or, in this case, copy it. One way is to simply useallocandinitWithString:, as in the correct example you show; the other way would becopy.The Memory Management Programming Guide for Cocoa explains everything. Every Cocoa or Cocoa Touch programmer should read or re-read it from time to time.