FROM : Samvel
DATE : Tue Mar 25 05:19:39 2008
Hi,
Lots of classes define static methods that create objects for instance:
Class NSString:
+(id) stringWithString: (NSString *) string;
or Class NSArray:
+(id) arrayWithObjects: (id) firstObj, ...;
I am really confused about next situation and memory management of
objects created via those methods. Please, explain.
Imagine class with
NSString *string;
instance variable. I create it and initiate with method above in:
-(id) init;
of my class. Now, want to use this string in another method sometime
later, say in:
-(void) plug;
Should I retain string in init?
e.g.:
-(id) init
{
// ...
string = [NSString stringWithString: @"hello"];
[string retain];
// ...
}
-(void) dealloc
{
// ...
[string dealloc];
// ...
}
-(void) plug
{
// ...
NSLog( "%@", string);
// ...
}
Thanks, Samvel.
DATE : Tue Mar 25 05:19:39 2008
Hi,
Lots of classes define static methods that create objects for instance:
Class NSString:
+(id) stringWithString: (NSString *) string;
or Class NSArray:
+(id) arrayWithObjects: (id) firstObj, ...;
I am really confused about next situation and memory management of
objects created via those methods. Please, explain.
Imagine class with
NSString *string;
instance variable. I create it and initiate with method above in:
-(id) init;
of my class. Now, want to use this string in another method sometime
later, say in:
-(void) plug;
Should I retain string in init?
e.g.:
-(id) init
{
// ...
string = [NSString stringWithString: @"hello"];
[string retain];
// ...
}
-(void) dealloc
{
// ...
[string dealloc];
// ...
}
-(void) plug
{
// ...
NSLog( "%@", string);
// ...
}
Thanks, Samvel.
| Related mails | Author | Date |
|---|---|---|
| Samvel | Mar 25, 05:19 | |
| Ken Thomases | Mar 25, 05:45 | |
| Jens Alfke | Mar 25, 05:54 | |
| Sherm Pendley | Mar 25, 05:54 | |
| Samvel | Mar 25, 05:56 |






Cocoa mail archive

