我的設計本能告訴我,如果得分的身份與其單純的價值不同,那麼應該排序某種得分對象而不是普通的NSNumbers。
但是那邊:在一個捏,你可以使用普通的NSValue類似於你如何使用NSNumber。獲取值需要多一點工作,但是NSValue本身並沒有實例合併行爲NSNumber對於小值。
是行使所有三種行爲的某些代碼:
// NSValues are always distinct:
int foo = 5, bar = 5, outfoo, outbar;
NSValue *one = [NSValue value:&foo withObjCType:@encode(int)];
NSValue *two = [NSValue value:&bar withObjCType:@encode(int)];
[one getValue:&outfoo];
[two getValue:&outbar];
NSLog(@"one: %@ %x = %d ; two: %@ %x = %d",
[one class], one, outfoo,
[two class], two, outbar);
// by comparison with NSNumber behavior:
NSNumber *three = [NSNumber numberWithInt:6];
NSNumber *four = [NSNumber numberWithInt:6];
NSLog(@"three: %@ %x = %d ; four: %@ %x = %d",
[three class], three, [three intValue],
[four class], four, [four intValue]);
// except when the numbers are big:
NSNumber *five = [NSNumber numberWithInt:8675309];
NSNumber *six = [NSNumber numberWithInt:8675309];
NSLog(@"five: %@ %x = %d ; six: %@ %x = %d",
[five class], five, [five intValue],
[six class], six, [six intValue]);
在我的Mac這會產生類似的輸出:
one: NSConcreteValue 42a8d0 = 5 ; two: NSConcreteValue 42a920 = 5
three: NSCFNumber 404380 = 6 ; four: NSCFNumber 404380 = 6
five: NSCFNumber 1324d0 = 8675309 ; six: NSCFNumber 106e00 = 8675309
你爲什麼關心,如果他們是同一個對象對與兩個不同的對象相同的價值? – 2010-11-24 18:15:40
因爲即使它們具有相同的值,我也需要它們作爲單獨的實例來計算分數。 – jchatard 2010-11-24 18:33:29