Question about NSArray property


#1

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; 
}

#2

Bump.

I’m also confused on this. I’ve read it 20 times and I’m still not sure why this works. The rest of the chapter makes sense to me but this is the one concept that has me stumped. :question:


#3

Actually I think this thread has the most clear explanation: viewtopic.php?f=457&t=8768