0

Is there such third-party extension of NSDate?

I need some methods to adjust date (increase or decrease day/month/year) and keep the date object in the same memory (so myDate = getNewDateByAdjustingOldOne (myDate); would not be acceptable).

If there is no already implemented mutable date, can it be possible to get internal NSDate data (considering that there is no access to internal data in NSDate class interface).

brigadir
  • 6,874
  • 6
  • 46
  • 81

3 Answers3

1

You could have all your objects that need the shared NSDate object store a pointer to the pointer, i.e.

NSDate** pDate

Then have all their operations perform on &pDate. In the class where you are adjusting the date, just keep resetting

&pDate = NSDate* *newDate*

All the objects will be in sync then.

rvijay007
  • 1,357
  • 12
  • 20
  • Understood... This looks like a wrapper above `NSDate`. Not so elegant as `NSDate` subclass but should work. – brigadir Mar 12 '13 at 18:07
  • Depends on how you look at it. A subclass is a whole separate class. In this case, you don't have any extra classes, which I would prefer to a separate wrapper class. :-) – rvijay007 Mar 13 '13 at 15:27
0

Really the memory overhead of doing myDate = getNewDateByAdjustingOldOne (myDate); is so small I really wouldn't worry about it. Premature optimisation can result in totally avoidable bugs.

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • The reason is that other objects keep reference to `myData` and I should sync them manually after adjusting. – brigadir May 16 '12 at 10:17
  • If you need to maintain all references intact, you can always create a wrapping object and use a pointer to that instead. Would be a good way to avoid having to update all references to your date. – diegoreymendez Jan 03 '13 at 12:51
0

You could use NSCalendar and NSDateComponents for a mutable dates. Either set up "pre-made" NSDateComponents to add/subtract defaults amounts of time or make them as needed.

NSDateComponents *comps = [NSDateComponents alloc] init];
comps.day = 1;
NSCalendar *cal = [NSCalendar currentCalendar];
// will wind up adding 1 day to the original date.
NSDate *newDate = [cal dateByAddingComponents:comps toDate:origDate options:NSWrapCalendarComponents];

Apple wrote some nice doc on Calendrical Calculations.

DBD
  • 23,075
  • 12
  • 60
  • 84