私のデザインの本能は、スコアのアイデンティティが単なる価値とは異なるものとして重要であるならば、プレーンなNSNumberの代わりに何らかの種類のスコアオブジェクトをソートするべきだと言います。
ただし、脇の下:ピンチでは、NSNumberの使用方法と同様にプレーンなNSValueを使用できます。値を取得する作業はもう少しですが、NSValue自体には小さい値に対してはNSNumberのインスタンスの結合動作がありません。
すべての3つの行動を行使するいくつかのコード:
// 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上で、これはのような出力が得られます。彼らは2つの異なるオブジェクト対同じオブジェクトである場合
one: NSConcreteValue 42a8d0 = 5 ; two: NSConcreteValue 42a920 = 5
three: NSCFNumber 404380 = 6 ; four: NSCFNumber 404380 = 6
five: NSCFNumber 1324d0 = 8675309 ; six: NSCFNumber 106e00 = 8675309
なぜあなたは気にしません同じ値ですか? –
同じ値を共有しているにもかかわらず、スコアを計算するために別々のインスタンスにする必要があるためです。 – jchatard