I wanna ask if I allocated an instance variable for private use in that class, should i release it immediately on site, or i can depend on dealloc function. (because maybe i will need it on other function) ?
//Player.h
@interface Player : NSObject
{
NSMutableArray * objectArray;
}
- (void)awake;
- (void)add;
@end
//Player.m
@implementation Player : NSObject
{
-(id) init {
self = [super init];
if (self != nil ){
[self awake];
[self add];
}
return self;
}
- (void) awake {
objectArray = [[NSMutableArray alloc] init]; //is it cause leakage?
[objectArray addObject:@"foobar"];
}
- (void) add {
[objectArray addObject:@"foobar2"];
}
- (void) dealloc {
[objectArray release];
[super dealloc];
}
}
@end
or should i using property to set the objectArray iVar?
//Player.h
@interface Player : NSObject
{
NSMutableArray * objectArray;
}
@property (nonatomic,retain)NSMutableArray* objectArray;
- (void)awake;
- (void)add;
@end
//Player.m
@implementation Player : NSObject
{
-(id) init {
self = [super init];
if (self != nil ){
[self awake];
[self add];
}
return self;
}
- (void) awake {
self.objectArray = [[NSMutableArray alloc] init autorelease]; //cause leakage?
[objectArray addObject:@"foobar"];
}
- (void) add {
[objectArray addObject:@"foobar2"];
}
- (void) dealloc {
[objectArray release];
[super dealloc];
}
}
@end
if both of them doesn't cause a leakage, what type should i use?
should i always set iVar property, and access iVar value with self even if i only want to use it in this class?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…