1

I need to save two date (a date range) to NSUserDefaults.

The range needs to be unique in the list of dates stored ?

Jules
  • 7,568
  • 14
  • 102
  • 186

2 Answers2

0

You can save NSDate objects to the user defaults. Do something like this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:firstDate forKey:@"StartDate"];
[defaults setObject:secondDate forKey:@"EndDate"];
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • How do i keep the list unique, so that the same two dates don't get saved twice. – Jules Jan 26 '11 at 15:00
  • @Jules: That'll have to be done in code, but the simple check would be comparing the start date of the second range to see if it is >= the start date of the first range and <= the end date of the first range. – mipadi Jan 26 '11 at 15:05
  • so you mean I'd have to read all the values from NSUserDefaults, evaluate them and save in necessary ? – Jules Jan 26 '11 at 15:21
  • @Jules: Yeah. `NSUserDefaults` won't do any uniqueness validation for you. You'll have to handle that logic yourself. – mipadi Jan 26 '11 at 16:25
0

That's based on this question of yours, right? The DateRange class provided as my answer to that question implements -isEqual: and -hash methods, so you can use DateRange objects in an NSSet. NSSet guarantees uniqueness of its members based on -isEqual: (see NSSet's documentation for details). All you need is to put a number of DateRange objects into an NSMutableSet, encode the set with NSKeyedArchiver and store the resulting NSData object in NSUserDefaults.

Community
  • 1
  • 1
Costique
  • 23,712
  • 4
  • 76
  • 79