FROM : Scott Thompson
DATE : Mon Jul 10 16:21:01 2006
On Jul 7, 2006, at 7:42 PM, Thomas Davie wrote:
> That is correct, but can you specify for me an NSArray containing
> only items that respond to a specific display method?
>
> This is the sort of check that I would like to be able to do at
> compile time.
What you are asking is, "Show me how to build a bus that can only
carry people that are at a particular convention".
In other words, it's not the Array's job to know what kinds of things
it contains. It just holds things and transports them from place to
place. If you wish to provide additional constraints on the things
that can go into an array, you need a gatekeeper... someone checking
for conference credentials before allowing someone onto the bus.
You've created a special type of array and your code will have to
reflect that.
The one way to do that is to wrap the NSArray in another class (a
ConferenceBus class if you will). Then the ConferenceBus class can
use a protocol declaration (id<ConferenceAttendee>) to ensure that
everyone who can get onto the bus has the proper credentials.
With Objective-C you could handle that by subclassing NSArray, or by
simply creating and object that contains an NSArray and have it
respond to all the methods that you need for it to so that it can be
substituted in the place of an NSArray at any point.
Scott
DATE : Mon Jul 10 16:21:01 2006
On Jul 7, 2006, at 7:42 PM, Thomas Davie wrote:
> That is correct, but can you specify for me an NSArray containing
> only items that respond to a specific display method?
>
> This is the sort of check that I would like to be able to do at
> compile time.
What you are asking is, "Show me how to build a bus that can only
carry people that are at a particular convention".
In other words, it's not the Array's job to know what kinds of things
it contains. It just holds things and transports them from place to
place. If you wish to provide additional constraints on the things
that can go into an array, you need a gatekeeper... someone checking
for conference credentials before allowing someone onto the bus.
You've created a special type of array and your code will have to
reflect that.
The one way to do that is to wrap the NSArray in another class (a
ConferenceBus class if you will). Then the ConferenceBus class can
use a protocol declaration (id<ConferenceAttendee>) to ensure that
everyone who can get onto the bus has the proper credentials.
With Objective-C you could handle that by subclassing NSArray, or by
simply creating and object that contains an NSArray and have it
respond to all the methods that you need for it to so that it can be
substituted in the place of an NSArray at any point.
Scott






Cocoa mail archive

