FROM : Michael Ash
DATE : Sun Jul 23 05:07:08 2006
On 7/22/06, patrick machielse <<email_removed>> wrote:
> Op 23-jul-2006, om 00:53 heeft Andre het volgende geschreven:
>
> > patrick machielse wrote:
> >
> >> @implementation NSArray (IdenticalObjects)
> >> - (BOOL)allObjectsIdentical
> >> {
> >> return [[NSSet setWithArray:self] count] == 2;
> >> }
> >> @end
>
> This should of course have read:
>
> >> return [[NSSet setWithArray:self] count] == 1; // _ONE_!
>
> A better/nicer/more general idea would be to use:
>
> - (unsigned)numberOfDistinctObjects
> {
> return [[NSSet setWithArray:self] count];
> }
I feel compelled to point out that this will not work for all arrays.
In particular, any arrays which contain objects whose -hash method is
incorrectly implemented will potentially return incorrect (inflated)
counts for the sets. This could happen if, for example, you have a
custom class that overrides -isEqual: but doesn't override -hash to
return a hash based on the equality criteria.
I would consider this a bug in the custom class, though. If you
override -isEqual: then you should always override -hash to match,
exactly because of this sort of problem.
Mike
DATE : Sun Jul 23 05:07:08 2006
On 7/22/06, patrick machielse <<email_removed>> wrote:
> Op 23-jul-2006, om 00:53 heeft Andre het volgende geschreven:
>
> > patrick machielse wrote:
> >
> >> @implementation NSArray (IdenticalObjects)
> >> - (BOOL)allObjectsIdentical
> >> {
> >> return [[NSSet setWithArray:self] count] == 2;
> >> }
> >> @end
>
> This should of course have read:
>
> >> return [[NSSet setWithArray:self] count] == 1; // _ONE_!
>
> A better/nicer/more general idea would be to use:
>
> - (unsigned)numberOfDistinctObjects
> {
> return [[NSSet setWithArray:self] count];
> }
I feel compelled to point out that this will not work for all arrays.
In particular, any arrays which contain objects whose -hash method is
incorrectly implemented will potentially return incorrect (inflated)
counts for the sets. This could happen if, for example, you have a
custom class that overrides -isEqual: but doesn't override -hash to
return a hash based on the equality criteria.
I would consider this a bug in the custom class, though. If you
override -isEqual: then you should always override -hash to match,
exactly because of this sort of problem.
Mike
| Related mails | Author | Date |
|---|---|---|
| listposter | Jul 22, 17:09 | |
| Ashley Clark | Jul 22, 19:12 | |
| Andre | Jul 22, 19:29 | |
| Adam R. Maxwell | Jul 22, 19:48 | |
| Andre | Jul 22, 20:08 | |
| patrick machielse | Jul 22, 21:25 | |
| Andre | Jul 23, 00:53 | |
| patrick machielse | Jul 23, 01:07 | |
| Andre | Jul 23, 04:14 | |
| Michael Ash | Jul 23, 05:07 |






Cocoa mail archive

