• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Re: Finding out if an NSArray's contents are all equal
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: Finding out if an NSArray's contents are all equal


  • Subject: Re: Re: Finding out if an NSArray's contents are all equal
  • From: "Michael Ash" <email@hidden>
  • Date: Sat, 22 Jul 2006 23:07:08 -0400

On 7/22/06, patrick machielse <email@hidden> 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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Finding out if an NSArray's contents are all equal (From: patrick machielse <email@hidden>)
 >Re: Finding out if an NSArray's contents are all equal (From: Andre <email@hidden>)
 >Re: Finding out if an NSArray's contents are all equal (From: patrick machielse <email@hidden>)

  • Prev by Date: Any way to test NSEvents within OCTest?
  • Next by Date: Re: Matrix Cell Rollovers
  • Previous by thread: Re: Finding out if an NSArray's contents are all equal
  • Next by thread: Found a bug in Cocoa: where should I report it?
  • Index(es):
    • Date
    • Thread