-1

I have a problem if anyone can help me or can give me an advice whose who faced with similar problem. Ok, I save data in NSUsersDefaults like this

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:getDate];
[defaults setObject:data forKey:@"data"];
[defaults synchronize];

and now when I want to retrieve in this way:

NSData *theData = [[NSUserDefaults standardUserDefaults] objectForKey:@"data"];
NSString *date = (NSString *) [NSKeyedUnarchiver unarchiveObjectWithData:theData];

if (date != nil) {
    NSMutableArray *mutable = [[NSMutableArray alloc] init];
    [mutable addObject:date];
}

is showing just last value, the value is picked from pickerDate. How can I make to save all dates that I'm picking from pickerDate. Thank you very much!!!

Joseph Duffy
  • 4,566
  • 9
  • 38
  • 68
Dan Paschevici
  • 961
  • 2
  • 10
  • 22

4 Answers4

1

i dont think that is his problem. NSUSerdefaults can be used to store date- i have seen it being used right here in stack, that i have used too:

NSUserDefaults *dateData = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];

[dateData setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];

and he is usingNSMutablearray- so that is not the problem either. The problem is one of two:

1: The problem could also be that the array is being initialised inside the loop, and previous values being saved in it are being deleted.

2: i think the problem is the location where the code to save the data into the array is located. I think you need to call the method to save the date into the array once the user is finished with selecting a date- the problem is that i dont think that datepicker has a delegate method that can know that the user has done so.

(

there is one that knows when any one field has been changed:

[self.myDatePicker addTarget:self
                      action:@selector(myAction:)
            forControlEvents:UIControlEventValueChanged];

But this will not help you as this will be called every time a field is changed- that is: it will call once you change the month field, once when you change the day field and so on.

)

Maybe you can include a done button into the datepicker, and call a method that will store the date into the array when the user clicks on this button...

s5v
  • 495
  • 5
  • 20
  • The second point that you mentioned here i think is correct but how to implement, can you say to me how??? – Dan Paschevici Mar 20 '15 at 11:27
  • http://stackoverflow.com/questions/6231587/iphone-adding-a-done-button-within-a-pop-up-datepicker-frame and http://www.mysamplecode.com/2012/12/ios-uidatepicker-example-done-button.html will both help you do that. – s5v Mar 20 '15 at 11:43
  • Thank you very much, it helped me to find the answer. I will post the answer soon. – Dan Paschevici Mar 22 '15 at 17:12
1

The problem is that you're not using the NSMutableDictionary correctly. Look at this code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:getDate];
[defaults setObject:data forKey:@"data"];
[defaults synchronize];

This means that you will always save the date under the key data. Because you're using a NSString, this means that if there is already an existing entry under data, it will overwrite it. For example:

The first time you use your app, there is nothing under data saved. I pick "1-20-2015", then my dictionary is:

{ data: "1-20-2015"}

Now I go and pick another date, "2-21-2015". I already have an entry under data, so this means it updates it to:

{ data: "2-21-2015"}

See how we erased "1-20-2015"? This is what's happening to you.

In a Dictionary, keys are unique objects. You need to use an NSMutableArray as the object, or use a unique key for each entry.

LyricalPanda
  • 1,424
  • 14
  • 25
0

Userdefaults is not for data storage. It is for user preferences. Somehow I think "all dates I'm picking from pickerDate" is not user preferences.

Instead of trying to store an NSDate, you can just store an NSNumber @(date.timeIntervalSinceReferenceDate). Makes it a lot easier.

And if you want to store more than one date - that's what an NSArray is there for.

Dennis
  • 2,119
  • 20
  • 29
gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Yes, you are right that UserDefaults is not for data storage, but I'm a lil bit confusing, can you develop your idea. thank you very much. – Dan Paschevici Mar 20 '15 at 10:58
0

I think you saving date in NSdate formate and getting date in NSString.

Do like this

NSData *theData = [[NSUserDefaults standardUserDefaults] objectForKey:@"data"];
 NSDate *date =[NSKeyedUnarchiver unarchiveObjectWithData:theData];
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36