I am a new in objective-c, I want to know when I return an object like the code below, whether I should autorelease it like this:
- (NSArray *)loadCategory
{
NSArray *temp = [[[NSArray alloc] initWithObjects:c1, c2, nil] autorelease];
return temp;
}
More, When I use call function to get the return value, whether I should retain the objective?
- (void)viewDidLoad
{
category = [self loadCategory];
[category retain];
[super viewDidLoad];
}
Finally, can I replace with:
- (void)viewDidLoad
{
self.category = [self loadCategory];
[super viewDidLoad];
}
Your code is fine.
You could alternatively use…
This will return an auto-release instance for you.
Also don’t forget to call release on this array if you retain it after you no longer need it.