NSMutableArray *_assets and (NSArray *)assets

Hi all,

I know this has been asked a few times in this board but I want to clarify a few things.

Interface :

//Create an Ivar to hold a pointer to the mutable array of assets. { NSMutableArray *_assets; }

Implementation:

-(void)setAssets:(NSArray *)a
{
    _assets = [a mutableCopy];
}

- (NSArray *)getAssets
{
    return [_assets copy];
}

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 :smiley: )

Thanks!

[quote]- (NSArray *)getAssets { return [_assets copy]; }[/quote]
Did the book actually ask you to write the getter like this?

Hi ibex10,

The book writes it as the following:

[code]//Accessors for assets properties

  • (void)setAssets:(NSArray *)a
    {
    _assets = [a mutableCopy];
    }

  • (NSArray *)assets
    {
    return [_assets copy]
    }[/code]

Write all your getters just as the book does.

Although the following is technically correct, it goes against the established conventions.

[quote]- (NSArray *)getAssets { return [_assets copy]; }[/quote]

[quote=“ibex10”]Write all your getters just as the book does.

Although the following is technically correct, it goes against the established conventions.

[quote]- (NSArray *)getAssets { return [_assets copy]; }[/quote][/quote]

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.

Hi man, could you solve that? I’ve the same doubt and it doesn’t allow me continue with the book :frowning:
I hope your answer.
Thanks!

[quote=“ibex10”]Write all your getters just as the book does.

Although the following is technically correct, it goes against the established conventions.

[quote]- (NSArray *)getAssets { return [_assets copy]; }[/quote][/quote]

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?