Hello everyone!
Well I cracked my head since the last night in order to figure out this problem, but unfortunately I cannot do so.
So I really hope you all can help me with this.
In the book, under BNREmployee.m, the author writes the following codes:
//Accessors for assets properties
-(void)setAssets:(NSArray *)a
{
_assets = [a mutableCopy];
}
-(NSArray *)assets
{
return [_assets copy];
}
-(void)addAsset:(BNRAsset *)a
{
//Is asset nil?
if(!_assets) {
//Create the array
_assets = [[NSMutableArray alloc]init];
}
[_assets addObject:a];
}
The above coding looks fine, but one question strikes me hard:
Why do we need to initialise accessors for assets properties? [-(void)setAssets:(NSArray *)a, -(NSArray *)assets]
Instead of writing the above accessors first, why don’t we write only -(void)addAsset:(BNRAsset *)a?
Because inside -(void)addAsset:(BNRAsset *)a, we could create a NSMutableArray if _assets doesn’t exist, right?
So why do we need to take so much trouble writing -(void)setAssets:(NSArray *)a, -(NSArray *)assets in the first place?
Thank you.