I have a method:
-(NSArray *)doSomething{
NSArray *array = [[NSArray alloc] initWithObjects:@"Huy 1",@"Huy 2",@"Huy 3",nil];
[array release];
return array;
}
and
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = [self doSomething];
if(array&&array.count>0){
NSLog([NSString stringWithFormat:@"%@\n",[array objectAtIndex:1]]);
}
else{
NSLog(@"Null");
}
}
I thinks i released array on doSomething() so it won’t return NSArray which i created on doSomething(). I don’t know it still print “Huy 2”? Anybody can tell me why?
Memory Management Programming Guide
If this works with
releaseinstead ofautoreleaseit’s probably because the memory that the array was using has yet to be used by anything else. This is just chance. I suspect that if you allocate an object afterNSArray *array = [self doSomething];you will get unexpected results.