I thought that NSCountedSet
counted numB
and numC
twice in the frequency because they had the same value, so I created two Fraction
objects (not shown) from my class Fraction
, and I set their ivars
(numerator
, denominator
) to equal each others but the countForObject:
treated them as two distinct objects and counted their frequencies as one each. numA
and numB
pointed to different places in memory but share the same value, and the two Fraction
objects pointed to different places in memory but shared the same value. Why were the Number
objects treated as indistinct, but not the Fraction
objects?
#import <Foundation/Foundation.h>
#import "Fraction.h"
int main (int argc, char *argv[]) {
@autoreleasepool {
NSNumber *numA = [NSNumber numberWithInt: 1];
NSNumber *numB = [NSNumber numberWithInt: 2];
NSNumber *numC = [NSNumber numberWithInt: 2];
NSArray *array = [NSArray arrayWithObjects: numA, numB, numC, nil];
NSCountedSet *mySet = [[NSCountedSet alloc] initWithArray: array];
for (NSNumber *myNum in mySet) {
NSLog(@"Number: %i Frequency: %lu", [myNum intValue], [mySet countForObject: myNum]);
}
}
return 0;
}
2012-08-05 17:44:58.667 prog[1150:707] Number: 1 Frequency: 1
2012-08-05 17:44:58.669 prog[1150:707] Number: 2 Frequency: 2
In order for your custom class to be recognized by
NSCountedSet
and by the rest of Foundation, you must implement-isEqual:
and-hash
correctly. By default, they compare pointers, so two objects will never compare equal even if they represent the same data. A naïve implementation of-isEqual:
for aFraction
class would probably look like this:Note that this would not allow you to compare instances of your
Fraction
class against instances ofNSNumber
. Because-isEqual:
must be commutative and because equal objects must have the same hash value, you would need to provide an implementation that was compatible withNSNumber
(probably by subclassing it and usingNSNumber
's implementations.)