So in the interface or header file we declare a ivar of type NSMutableArray that will hold the address to a NSMutableArray.
In the implementation we modify the setter and getter method for the
-(void)setAssets:(NSArray *)a
{
_assets = [a mutableCopy];
}; this makes a mutable copy of the immutable assets that was declared in the property and assigns it to the ivar that holds the pointer to a NSMutableArray Object
I’m not exactly sure what this does.
- (NSArray *)getAssets
{
return [_assets copy];
}
but this is my guess:
Returns a copy of the pointer _assets to a NSMutableArray, but…since we used copy here the getter method will return a immutable NSArray?
Another question, since we created the ivar NSMutableArray *_assets in BNREmployee.h we will only be able to modify the array with a BNREmployee object right?
Can someone please comment and tell me if my statements are (true or false and please elaborate and correct me if I’m wrong, which I think I am )
Yup I noticed my mistake after I read it over a few more times. I’d like to follow established conventions as close as possible, thanks for pointing that out.
So were my statements in the first post correct? If not can you please go through and explain it to me?
Thank you for replying to these posts btw…I’ve noticed you’re active in these forums and have helped many out.
I just changed my getter to the above. But in main I am still using the dot notation as in someEmployee.assets. As far as I can tell, it worked fine. So what did the compiler do? I mean when it sees someEmployee.assets, is it using the accessor created from using the property assets. If so, is that getter simply return _assets? Would that return a mutable array? That seems confusing and I am surprised there is no kind of error since the property assets is of the type NSArray.
Update: I just read chapter 34. I think after reading it that the copy attribute would cause the compiler to synthesize a getter as in
return [_assets copy]. So I guess it would return what we originally had before changing the getter to getAssets. In that case, it seems we did not need to make these accessors in the first place (because of the copy attribute), but perhaps we did to learn about how to make them ourselves. Does that sound right?