Can someone tell me if I / I need to assign / release an instance of the NSString "planetName" instance (as in the example below) or is this done when the class instance is created / allocated?
I understand that int and float do not have to be, but are not sure about NSString and NSArray ...
@interface PlanetClass : NSObject {
NSString *planetName;
}
- (NSString *)planetName;
- (void)setPlanetName:(NSString *)value;
@end
Like this...
- (id) init {
[super init];
planetName = [[NSString alloc] init];
return self;
}
- (void) dealloc {
[planetName release];
[super dealloc];
}
** ---------------------------------- ** EDIT: EDIT: Here is another version ** - -------------------------------- **
int main(int argc, const char *argv[]) {
PlanetClass *newPlanet_01 = [[PlanetClass alloc] init];
NSString *newPlanetName = [NSString alloc] init];
newPlanetName = @"Jupiter";
[newPlanet_01 setPlanetName:newPlanetName];
[newPlanet_01 release];
return 0;
}
init and dealloc methods would be like this ...
- (id) init {
[super init];
return self;
}
- (void) dealloc {
[planetName release]
[super dealloc];
}
The setPlanetName method will look like this:
- (void)setPlanetName:(NSString *)newPlanetName {
if (planetName != newPlanetName) {
[planetName release];
planetName = [newPlanetName copy];
}
}
PS: I do not use properties or synthesize, I have not received this yet.
cheers -gary -
source
share