无法获取 NSDictionary 元素
Can't get NSDictionary element
我从 NSUserDefaults 中获取词典字典:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:keyForMemory];
NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSMutableDictionary * dictionaryFromMemory = [[[NSDictionary alloc] initWithDictionary:retrievedDictionary] mutableCopy];
int flag = 0;
for (NSDictionary * dict in dictionaryFromMemory) {
if ([[dictionary objectForKey:@"purchaseId"] isEqualToString:[dict objectForKey:@"purchaseId"]]) {
flag = 1;
}
}
这个小字典一定是一个 NSDictionary,但是当我在循环中获取日志时,我看到了以下内容:
不知道为什么,dict不是字典,只是一个字符串,怎么可能从那个大字典中取出字典,因为我的解决方案没有生命迹象(
dictionaryFromMemory 是字典的字典,它是这样创建和保存的:
NSMutableDictionary * bigDict = [[NSMutableDictionary alloc] init];
NSDictionary * smallDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[dictionary objectForKey:@"purchaseId"], @"purchaseId",
[dictionary objectForKey:@"price"], @"price",
[dictionary objectForKey:@"enable"],@"enable",
[dictionary objectForKey:@"lessons_number"],@"lessonNumber",
[NSString stringWithFormat:@"0"], @"purchased",
nil];
[bigDict setObject:smallDict forKey:[dictionary objectForKey:@"purchaseId"]];
NSMutableString * tempStr10 = [NSMutableString stringWithString:pairedData];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:[NSKeyedArchiver archivedDataWithRootObject:bigDict] forKey:tempStr10];
[def synchronize];
这个循环不正确:
for (NSDictionary * dict in dictionaryFromMemory) {
if ([[dictionary objectForKey:@"purchaseId"] isEqualToString:[dict objectForKey:@"purchaseId"]]) {
flag = 1;
}
}
dict
在此上下文中表示迭代中的当前 key,因此您看到 "com.ION.PSTutorial.b.SeriesAll".
的原因
更改循环以根据此键检索字典:
for (id key in dictionaryFromMemory) {
NSDictionary *dict = dictionaryFromMemory[key];
if ([dictionary[@"purchaseId"] isEqualToString:dict[@"purchaseId"]]) {
flag = 1;
}
}
我还冒昧地将字典访问转换为使用现代下标语法。
我从 NSUserDefaults 中获取词典字典:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:keyForMemory];
NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSMutableDictionary * dictionaryFromMemory = [[[NSDictionary alloc] initWithDictionary:retrievedDictionary] mutableCopy];
int flag = 0;
for (NSDictionary * dict in dictionaryFromMemory) {
if ([[dictionary objectForKey:@"purchaseId"] isEqualToString:[dict objectForKey:@"purchaseId"]]) {
flag = 1;
}
}
这个小字典一定是一个 NSDictionary,但是当我在循环中获取日志时,我看到了以下内容:
不知道为什么,dict不是字典,只是一个字符串,怎么可能从那个大字典中取出字典,因为我的解决方案没有生命迹象(
dictionaryFromMemory 是字典的字典,它是这样创建和保存的:
NSMutableDictionary * bigDict = [[NSMutableDictionary alloc] init];
NSDictionary * smallDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[dictionary objectForKey:@"purchaseId"], @"purchaseId",
[dictionary objectForKey:@"price"], @"price",
[dictionary objectForKey:@"enable"],@"enable",
[dictionary objectForKey:@"lessons_number"],@"lessonNumber",
[NSString stringWithFormat:@"0"], @"purchased",
nil];
[bigDict setObject:smallDict forKey:[dictionary objectForKey:@"purchaseId"]];
NSMutableString * tempStr10 = [NSMutableString stringWithString:pairedData];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:[NSKeyedArchiver archivedDataWithRootObject:bigDict] forKey:tempStr10];
[def synchronize];
这个循环不正确:
for (NSDictionary * dict in dictionaryFromMemory) {
if ([[dictionary objectForKey:@"purchaseId"] isEqualToString:[dict objectForKey:@"purchaseId"]]) {
flag = 1;
}
}
dict
在此上下文中表示迭代中的当前 key,因此您看到 "com.ION.PSTutorial.b.SeriesAll".
更改循环以根据此键检索字典:
for (id key in dictionaryFromMemory) {
NSDictionary *dict = dictionaryFromMemory[key];
if ([dictionary[@"purchaseId"] isEqualToString:dict[@"purchaseId"]]) {
flag = 1;
}
}
我还冒昧地将字典访问转换为使用现代下标语法。