Ios Can';无法获取NSDictionary元素
我从NSUserDefaults获得字典字典:Ios Can';无法获取NSDictionary元素,ios,objective-c,nsdictionary,nsuserdefaults,Ios,Objective C,Nsdictionary,Nsuserdefaults,我从NSUserDefaults获得字典字典: NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSData *data = [def objectForKey:keyForMemory]; NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data]; NSMutableDictionary * dictionar
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
表示迭代中的当前键,因此您会看到“com.ION.PSTutorial.b.SeriesAll”
更改循环以基于此键检索字典:
for (id key in dictionaryFromMemory) {
NSDictionary *dict = dictionaryFromMemory[key];
if ([dictionary[@"purchaseId"] isEqualToString:dict[@"purchaseId"]]) {
flag = 1;
}
}
我还冒昧地将字典访问转换为使用现代订阅语法。您没有提供足够的信息让我们帮助您。是什么让您认为该字典包含字符串而不是其他字典?在我看来,它就像一本字典字典。这本字典是如何创建的ed?你需要给我们看那段代码。@Duncac我编辑了这篇文章,并展示了这本词典是如何创建的。它遍历NSDictionary的键,键是一个NSString。要获得内部词典,你需要在截图
com.ION.PSTutorial.b.SeriesAll
中键的实际文字值