FROM : Damien Sorresso
DATE : Sat Jul 08 03:00:47 2006
On 7 Jul, 2006, at 7:50 PM, Erik Buck wrote:
>> One such example is the NSArray foundation class. I would like to be
>> able to express that I specifically have an NSArray containing only
>> objects of a certain type. This would allow me to (a) let the
>>
>
> Just out of curiosity, why do you want this ? How often do you put
> the wrong kind of object in a collection in practice ? What
> determines "wrong" ? Do you have some objection to -
> respondsToSelector: in cases when you can't know in advance what
> objects might be in a collection that was provided to your code ?
>
It seems curious, since when using an `NSArray', you're using an
immutable object. The only opportunity you'd have to screw up is
during initialization, which you really shouldn't. If type-checking
is necessary, do type-checking. That's what `isKindOfClass:' is for.
But in any case, you could easily sub-class `NSArray' or
`NSMutableArray' to do this. Just overload `NSMutableArray's
`addObject:' method to do something like this.
- (void)addObject:(id)anObject
{
if( [anObject isKindOfClass:_arrayClass] ) [super addObject:anObject];
else {
// Do whatever...
}
}
`_arrayClass' is, of course, an instance variable for your
`NSMutableArray' subclass.
--
Damien Sorresso
Mac OS X Developer
Computer Infrastructure Support Services
Illinois State University
E-mail: damien.<email_removed>
DATE : Sat Jul 08 03:00:47 2006
On 7 Jul, 2006, at 7:50 PM, Erik Buck wrote:
>> One such example is the NSArray foundation class. I would like to be
>> able to express that I specifically have an NSArray containing only
>> objects of a certain type. This would allow me to (a) let the
>>
>
> Just out of curiosity, why do you want this ? How often do you put
> the wrong kind of object in a collection in practice ? What
> determines "wrong" ? Do you have some objection to -
> respondsToSelector: in cases when you can't know in advance what
> objects might be in a collection that was provided to your code ?
>
It seems curious, since when using an `NSArray', you're using an
immutable object. The only opportunity you'd have to screw up is
during initialization, which you really shouldn't. If type-checking
is necessary, do type-checking. That's what `isKindOfClass:' is for.
But in any case, you could easily sub-class `NSArray' or
`NSMutableArray' to do this. Just overload `NSMutableArray's
`addObject:' method to do something like this.
- (void)addObject:(id)anObject
{
if( [anObject isKindOfClass:_arrayClass] ) [super addObject:anObject];
else {
// Do whatever...
}
}
`_arrayClass' is, of course, an instance variable for your
`NSMutableArray' subclass.
--
Damien Sorresso
Mac OS X Developer
Computer Infrastructure Support Services
Illinois State University
E-mail: damien.<email_removed>






Cocoa mail archive

