in books, we are told to write
[code]-(void)setAssets:(NSArray *)assets
{
_assets = [assets mutableCopy ];
}
-(NSArray *)assets
{
return [_assets copy];
}
[/code]
we did not declare it at BNREmployee.h but in .m file (those 2 are getter and setter / accessors)
why did we need to modify setter method? (when I comment those code (put //) the code still works fine. )
ibex10
2
To make a mutable copy of the incoming array object. (There is not a way to say “please make a mutable copy” in a property declaration.)
but why do we also need copy here?
-(NSArray *)assets
{
return [_assets copy];
}
can’t we just write??
-(NSArray *)assets
{
return _assets;
}
ibex10
4
[quote]but why do we also need copy here?
-(NSArray *)assets
{
return [_assets copy];
}
[/quote]
To avoid returning the object itself.
[quote]can’t we just write??
-(NSArray *)assets
{
return _assets;
}
[/quote]
You can, but then you will return the object itself.
See Also: Copying Collections:
developer.apple.com/library/mac … pying.html