FROM : Chris Hanson
DATE : Mon Jun 09 20:39:55 2008
On Jun 9, 2008, at 11:17 AM, Hamish Allan wrote:
> On Mon, Jun 9, 2008 at 7:02 PM, Chris Hanson <<email_removed>> wrote:
>
>> The reason these kinds of methods have a return type of (id) is
>> that there
>> is no way to say "returns an object of the receiver's class." For
>> example,
>> +[NSArray array] returns (id) rather than (NSArray *) because
>> otherwise
>> +[NSMutableArray array] would require a separate declaration
>> (NSMutableArray
>> *). Rather than have a large number of separate declarations,
>> these methods
>> return (id).
>
> If this is true, it rather contradicts your point, because it should
> be perfectly legal to return an NSMutableArray (a subclass of NSArray)
> from +(NSArray *)array.
It is perfectly legal to return an NSMutableArray from a hypothetical +
(NSArray *)array method.
However, all the sender of that +(NSArray *)array message can know is
that the result can be treated as an NSArray. It can't know whether
an NSArray or NSMutableArray is returned (unless it does extra work,
like using -isKindOfClass:, which also happens to be fragile).
This is actually used quite often in Cocoa development. For example,
if you have an Account class with a "transactions" array property that
you want to use with Key-Value Observing, you'll probably write its
interface to look something like this (if you implement all of the
various KVO methods):
@interface Account : NSObject {
NSMutableArray *transactions;
}
- (NSArray *)transactions;
- (void)setTransactions:(NSArray *)newTransactions;
- (void)insertObject:(id)object inTransactionsAtIndex:
(NSUInteger)index;
- (void)removeObjectFromTransactionsAtIndex:(NSUInteger)index;
@end
All of these methods would actually be implemented to return or
manipulate the NSMutableArray instance variable.
-- Chris
DATE : Mon Jun 09 20:39:55 2008
On Jun 9, 2008, at 11:17 AM, Hamish Allan wrote:
> On Mon, Jun 9, 2008 at 7:02 PM, Chris Hanson <<email_removed>> wrote:
>
>> The reason these kinds of methods have a return type of (id) is
>> that there
>> is no way to say "returns an object of the receiver's class." For
>> example,
>> +[NSArray array] returns (id) rather than (NSArray *) because
>> otherwise
>> +[NSMutableArray array] would require a separate declaration
>> (NSMutableArray
>> *). Rather than have a large number of separate declarations,
>> these methods
>> return (id).
>
> If this is true, it rather contradicts your point, because it should
> be perfectly legal to return an NSMutableArray (a subclass of NSArray)
> from +(NSArray *)array.
It is perfectly legal to return an NSMutableArray from a hypothetical +
(NSArray *)array method.
However, all the sender of that +(NSArray *)array message can know is
that the result can be treated as an NSArray. It can't know whether
an NSArray or NSMutableArray is returned (unless it does extra work,
like using -isKindOfClass:, which also happens to be fragile).
This is actually used quite often in Cocoa development. For example,
if you have an Account class with a "transactions" array property that
you want to use with Key-Value Observing, you'll probably write its
interface to look something like this (if you implement all of the
various KVO methods):
@interface Account : NSObject {
NSMutableArray *transactions;
}
- (NSArray *)transactions;
- (void)setTransactions:(NSArray *)newTransactions;
- (void)insertObject:(id)object inTransactionsAtIndex:
(NSUInteger)index;
- (void)removeObjectFromTransactionsAtIndex:(NSUInteger)index;
@end
All of these methods would actually be implemented to return or
manipulate the NSMutableArray instance variable.
-- Chris






Cocoa mail archive

