我只是想在 NSUserdefaults 中保存数组字典的字典,但没有成功。这是我的代码:
+ (BOOL) userHasAlreadyLoad:(NSNumber *)territoryId year:(NSInteger)year month:(NSInteger)month
{
NSMutableDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SETTING_MONTHS_LOADED];
NSMutableDictionary *dictOfTerritory = dict[[territoryId stringValue]];
if (dictOfTerritory == nil)
return NO;
NSMutableArray *yearData = dictOfTerritory[[@(year) stringValue]];
if (yearData == nil)
return NO;
if ([yearData containsObject:@(month)])
return YES;
return NO;
}
+ (void) setUserLoad:(NSNumber *)territoryId year:(NSInteger)year month:(NSInteger)month
{
NSMutableDictionary *dict = [[[NSUserDefaults standardUserDefaults] objectForKey:USER_SETTING_MONTHS_LOADED] mutableCopy];
NSMutableDictionary *dictOfTerritory = [dict[[territoryId stringValue]] mutableCopy];
if (dictOfTerritory == nil)
{
dictOfTerritory = [NSMutableDictionary dictionary];
dict[[territoryId stringValue]] = dictOfTerritory;
}
NSMutableArray *yearData = [dictOfTerritory[[@(year) stringValue]] mutableCopy];
if (yearData == nil)
{
yearData = [NSMutableArray array];
}
if (![yearData containsObject:@(month)])
{
[yearData addObject:@(month)];
}
dictOfTerritory[[@(year) stringValue]] = yearData;
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:USER_SETTING_MONTHS_LOADED];
[[NSUserDefaults standardUserDefaults] synchronize];
}
当我第一次运行应用程序时,它会正确保存数据,这意味着yearData
它将包含一个月,但是在向该数组添加更多数据后,我在调用 userHasAlreadyLoad 时找不到它们;我只能看到同一区域 ID 和年份的一个月,这是我添加的第一个月。我在这里错过了什么?