简体   繁体   English

无法在NSUserDefault中存储NSMutableDictionary

[英]Not able to store NSMutableDictionary in NSUserDefault

Hi i know this question is asked before i wanted to know where i am going wrong in my code.So i am asking this question .. I am trying to store the value of nsmutabledictionary into nsuserdefaults .. 嗨,我知道这个问题是在我想知道我的代码在哪里出问题之前问过的。所以我在问这个问题..我正在尝试将nsmutabledictionary的值存储到nsuserdefaults中。

I have added uiswitch in uitableviewcell . 我在uitableviewcell中添加了uiswitch。 so each time i turn ON uiswitch it should get stored in nsuserdefaults so that when i quit the app and reopen the app the switch which was ON the last time should be switched ON 因此,每次我打开uiswitch时,它都应该存储在nsuserdefaults中,以便当我退出该应用程序并重新打开该应用程序时,应该将上次打开的开关打开

- (void)viewDidLoad
{
    [super viewDidLoad];

    activeNotificationDictionary = [[NSMutableDictionary alloc]init];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:@"theKey"];
    NSMutableDictionary *artworkDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];

    artworkDict = activeNotificationDictionary;

}

-(void)notificationChangedForSymptom:(int)symptomIDNo withRemedyID:(int)remedyID isSelected:(BOOL)isSelected
{
    if (isSelected == YES)
    {        
        selectedSymptomID = symptomIDNo;
        selectedRemedyID = remedyID;

        if ([activeNotificationDictionary objectForKey:[NSNumber numberWithInt:symptomIDNo]] != nil)
       {
           UIAlertView *selectedNotification = [[UIAlertView alloc]initWithTitle:@"Reminder" message:@"Would you like to change the notification" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

           selectedNotification.delegate = self;
           NSLog(@" selected remedyID for symptom %@", activeNotificationDictionary);

           [selectedNotification show];

           NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

           NSDictionary *dict = [NSDictionary dictionaryWithObject:activeNotificationDictionary forKey:@"dictKey"];
           NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict];

           [defaults setObject:data forKey:@"theKey"];

           [defaults synchronize];

           NSLog(@"default value %@",defaults);

       }
       else 
       {

           [activeNotificationDictionary setObject:[NSNumber numberWithInt:RemedyID] forKey:[NSNumber numberWithInt:SymptomIDNo]];

       }

    }       

    else

      {
          [activeNotificationDictionary removeObjectForKey:[NSNumber numberWithInt:symptomIDNo]];
      }
}

When you encode a mutable dictionary just an immutable dictionary gets encoded, because NSMutableDictionary isn't overriding NSSecureCoding methods. 对可变字典进行编码时,仅对不可变的字典进行编码,因为NSMutableDictionary不会覆盖NSSecureCoding方法。 So make a mutable copy of it: 因此,制作一个可变的副本:

NSData *data = [defaults objectForKey:@"theKey"];
NSMutableDictionary *artworkDict = [[NSKeyedUnarchiver unarchiveObjectWithData:data]mutableCopy];

EDIT 编辑

I suggest to initialize every default before the app starts, so for example: 我建议在应用启动之前初始化每个默认值,例如:

+ (void) initialize // Inside the class storing the defaults
{
    NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
    [defaults registerDefaults: @{ @"theKey":@{} } ];
}

If you want to synchronize all the defaults with the database, call synchronize: 如果要将所有默认值与数据库同步,请调用sync:

[defaults synchronize];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM