これはおそらくかなり基本的な問題ですが、原因を突き止めることができないので試してみてください。
次のコードを書きました。
NSMutableDictionary * myDictionary = [[NSMutableDictionary alloc] init];
NSMutableArray * values = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
// create an object that can become a key in an NSDictionary
CustomObjectAdoptingNSCopy * someKey = [[CustomObjectAdoptingNSCopy alloc] init];
// create a random value that will become an object for myDictionary ...
int someValue = rand()%10;
// ... and which also gets to be an object in the values array, provided it isn't already stored there
if ([values indexOfObject:[NSNumber numberWithInt:someValue]]==NSNotFound)
[values addObject:[NSNumber numberWithInt:someValue]];
// now associate someValue with someKey in myDictionary
[myDictionary setObject:[NSNumber numberWithInt:someValue] forKey:someKey];
}
// read the data in myDictionary enumerating the NSArray values
for (NSNumber * aValue in values){
NSArray * objectsForValue = [myDictionary allKeysForObject:aValue];
// now the data from objectsForValue are being used ...
}
この問題objectsForValue
は が空のときに発生します。これは常に発生するわけではありませんが、定期的に発生します。私があまり間違っていなければ[NSNumber numberWithInt:someValue]
、 Object に に追加されたときに NSNull として解釈されない限り、これは論理的に不可能ですmyDictionary
が、同じ式が NSArray に追加されているときにオブジェクトとして扱われますvalues
。
そしてロギングするmyDictionary
と、これが実際に起こっていることに気づきました。誰かが私に理由を説明できますか?