Re: Error in learning Cocoa book?
Re: Error in learning Cocoa book?
- Subject: Re: Error in learning Cocoa book?
- From: Brendan Younger <email@hidden>
- Date: Sat, 13 Oct 2001 13:00:35 -0400
On Saturday, October 13, 2001, at 11:45 AM, Ricky Sharp wrote:
Let's assume that callers may or may not need mutable forms of the
array and the return type was made NSArray* in both cases.
if I then have...
NSArray* arrayOne = [anInstanceOfExpenses expenses];
NSMutableArray* arrayTwo = [anInstanceOfExpenses expenses];
What happens here? Similar question to if the Expenses class has the
return type of NSMutableArray...what happens?
Both cases will work. Changing the return type does not in any way
change the class which is being returned. In this case, though,
NSMutableArray *is* an NSArray (since one is a subclass of the other) so
"changing" the return type is really just hiding some of the information
about expenses. What changing the return type does is tell the caller
what sort of object to expect. This is in keeping with OOP principles
in that the caller should *not* depend on any internal representation of
the instance variable expenses. If expenses were changed so as to be
implemented as a NSArray, then arrayOne would still be fine but arrayTwo
would break. Therefore, one should only care what the return type is,
not what it actually is.
Brendan Younger