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