I’ve gone through the other topics about the use of the NSArray property, but I’m still confused. How exactly does making an instance variable of NSMutableArray and a property of type NSArray prevent other classes from receiving a non-mutable array? Also, how can you differentiate the accessor methods of the ivar NSMutable Array and NSArray property if they both have the same pointer name “_assets”. Why do we need the NSArray property?
Here’s the code to use as context for my previous questions
// Accessors for assets properties
- (void) setAssets:( NSArray *) a
{
_assets = [a mutableCopy];
}
- (NSArray *) assets
{
return [_assets copy];
}
- (void) addAsset:( BNRAsset *) a
{ // Is assets nil?
if (! _assets) {
// Create the array _assets = [[ NSMutableArray alloc] init];
}
[_assets addObject:a];
}
- (unsigned int) valueOfAssets
{
// Sum up the resale value of the assets
unsigned int sum = 0;
for (BNRAsset *a in _assets) {
sum + = [a resaleValue];
}
return sum;
}