Ios 无法在NSUserDefault中存储NSMutableDictionary

Ios 无法在NSUserDefault中存储NSMutableDictionary,ios,objective-c,nsuserdefaults,nsmutabledictionary,Ios,Objective C,Nsuserdefaults,Nsmutabledictionary,嗨,我知道这个问题是在我想知道我的代码哪里出错之前问的。所以我问这个问题。。我正在尝试将nsmutabledictionary的值存储到nsuserdefaults中 我在uitableviewcell中添加了uiswitch。因此,每次我打开uiswitch时,它都应该存储在nsuserdefaults中,这样当我退出应用程序并重新打开应用程序时,上次打开的开关应该打开 - (void)viewDidLoad { [super viewDidLoad]; activeNoti

嗨,我知道这个问题是在我想知道我的代码哪里出错之前问的。所以我问这个问题。。我正在尝试将nsmutabledictionary的值存储到nsuserdefaults中

我在uitableviewcell中添加了uiswitch。因此,每次我打开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]];
      }
}

对可变字典进行编码时,只会对不可变字典进行编码,因为
NSMutableDictionary
不会覆盖
NSSecureCoding
方法。因此,制作它的可变副本:

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

我建议在应用程序启动之前初始化每个默认值,例如:

+ (void) initialize // Inside the class storing the defaults
{
    NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
    [defaults registerDefaults: @{ @"theKey":@{} } ];
}
如果要将所有默认值与数据库同步,请调用同步:

[defaults synchronize];

对可变字典进行编码时,只会对不可变字典进行编码,因为
NSMutableDictionary
不会覆盖
NSSecureCoding
方法。因此,制作它的可变副本:

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

我建议在应用程序启动之前初始化每个默认值,例如:

+ (void) initialize // Inside the class storing the defaults
{
    NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
    [defaults registerDefaults: @{ @"theKey":@{} } ];
}
如果要将所有默认值与数据库同步,请调用同步:

[defaults synchronize];

即使这样做,开关也不会被存储到以前的状态。。。知道我哪里出错了吗?viewDidLoad后是否调用notificationChangedForSymptom?如果是这样,您仍然没有任何对象存储在其上。我建议使用initialize方法注册默认值。它在自定义单元格中调用。它是一个委托方法。。如何初始化方法在此行中放置断点:
NSData*data=[defaults objectForKey:@“theKey”]。如果返回
nil
,则您尚未注册默认值,我将告诉您如何注册;即使这样做,开关也不会被存储到以前的状态。。。知道我哪里出错了吗?viewDidLoad后是否调用notificationChangedForSymptom?如果是这样,您仍然没有任何对象存储在其上。我建议使用initialize方法注册默认值。它在自定义单元格中调用。它是一个委托方法。。如何初始化方法在此行中放置断点:
NSData*data=[defaults objectForKey:@“theKey”]。如果返回
nil
,则您尚未注册默认值,我将告诉您如何注册;