-(NSArray *)allPossessions


#1

I have a question about the -(NSArray *)allPossessions method:
I’m assuming this is a getter method for the allPossessions ivar? What threw me off was that the return type is (NSArray *) and not (NSMutableArray *). What’s the reason for having the getter’s return type be NSArray * and not NSMutableArray *? And I guess Objective-C is okay with the getter’s return type being different that that of the ivar’s?

Thanx in advance!


#2

The caller of the getter doesn’t need to know that the allPossessions array returned to him is actually mutable. We don’t want him to modify the NSMutableArray our instance variable points to, after all. (The getter returns a pointer to the very same array our instance variable points to, not a copy of it.) If he wanted to modify the returned array, he should make a mutable copy of it.

This is perfectly legal Objective-C because NSMutableArray inherits from NSArray, and in place of a certain type, you can use any one of its subtypes. (In other words, “NSMutableArray is an NSArray.”)