この非常に苛立たしい、しかし明らかに単純な問題で私が間違っている場所を誰かが説明してくれることを願っています.
viewDidLoad で NSMutable 辞書をインスタンス化しています。
- (void)viewDidLoad
{
[super viewDidLoad];
self.myDictionary = [[NSMutableDictionary alloc] init];
// その他のコードはこちら }
次に、辞書に入力します。
[self.myDictionary setObject:[NSNumber numberWithInt:holidayDays] forKey:[self.holidayInfo objectForKey:@"days"]];
しかし、特定のキーの値を取得しようとすると、NULL が返されます。
- (void) processHolidayLoop:(int)i forAKey:(NSString *)aKey using:(UIManagedDocument *)document
{
if ([self.myDictionary respondsToSelector:@selector(objectForKey:)]) { NSLog(@"YES IT DOES");}
int hDays = [[self.myDictionary objectForKey:aKey] intValue];
NSLog(@"aKey is %@", aKey);
NSLog(@"hDays is %d", hDays);
NSLog(@"myDictionary valueForKey: is %@", [[self.myDictionary objectForKey:aKey] description]);
// more code here
}
上記のコード NSLogs、YES IT DOES、aKey は正しく動作しますが、別のメソッドから呼び出されると、"hDays is (null)" および "myDictionary valueForKey: is (null)" が返されます。
NSString *aKey = [NSString stringWithFormat:@"holiday%d", j ];
[self processHolidayLoop:i forAKey:aKey using:self.document];
次の方法を使用して self.myDictionary の値とキーを確認し、ディクショナリにすべてのキーと値が正しく入力されていることを確認しました。
- (void) describeDictionary:(NSDictionary *)dict
{
NSArray *keys;
int i, count;
id key, value;
keys = [dict allKeys];
count = [keys count];
for (i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [dict objectForKey: key];
NSLog (@"Key: %@ has value: %@", key, value);
}
}
私はこれに関するすべてのSOの回答に目を通し、これを解決するさまざまな方法を試しましたが、うまくいきません。非常に明白な何かが欠けていると確信していますが、他の誰かに見てもらう必要があります。
前もって感謝します!