こんにちは私はこの質問が私のコードのどこで間違っているのか知りたい前に尋ねられることを知っています。それで私はこの質問をしています..私は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]];
}
}