Say I have two properties defined as such:
@property (nonatomic, strong) UITableView *parentTableView;
@property (nonatomic, strong) NSMutableArray *headersArray;
and a method:
-(void)prepareTags;
and say I have an init method like this:
-(id)initWithParentTableView:(UITableView*)parentTable
{
if(self = [super init])
{
//1
NSMutableArray *array = [[NSMutableArray alloc] init];
headersArray = array;
//2
self.parentTableView = parentTable;
//3
[self prepareTags];
}
return self;
}
self.parentTableView from the init method?self too. Will self be ready to use, even though the init method hasn't returned yet?Respectively (I'd use list formatting but I can't make it work with blockquotes...!):
Yes, but there's no point having the array variable, you might as well just do: headersArray = [[NSMutableArray alloc] init];
self.parentTableView from the init method?No, to quote the Apple docs on Practical Memory Management:
Don’t Use Accessor Methods in Initializer Methods and dealloc
. You should access the ivar directly (as you do with headersArray)
init method (in this case, the prepareTags method calls self too. Will self be ready to use, even though the init method hasn't returned yet?Yes. Just be very careful (your object may not have been fully initialised, it shouldn't use accessor methods so as to comply with the previous restriction, et cetera)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With