0

I am trying to convert a NSString to a date, add a day to it, then convert back to a string.

My code so far is this:

//convert curDate (from @property declaration) to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:curDate];

//add a day
dateFromString = [dateFromString dateByAddingTimeInterval:60*60*24*1];

//convert back to string (for use in URL string concatenation)
NSString *dateDisplay = [dateFormatter stringFromDate:dateFromString];

//set curDate to new date
curDate = dateDisplay;

It crashes to the stack at:

0x117f09b:  movl   8(%edx), %edi

(not sure if that's useful).

Can anyone say why this is?

Thanks!

samiles
  • 3,768
  • 12
  • 44
  • 71
  • put your code in `@try` `@catch` blocks and print out the exception. – pbibergal Jul 01 '13 at 12:16
  • 3
    Better if you add a symbolic breakpoint for any exception thrown. However I tried your code and it doesn't throw any exception. – Ramy Al Zuhouri Jul 01 '13 at 12:18
  • 1
    Probably the date format does not match the string and `dateFromString` is `nil`. - Single-stepping in the debugger (and inspecting the variables) can be extremely helpful! – Martin R Jul 01 '13 at 12:20
  • `NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:curDate];` should be `NSDate *dateFromString = [dateFormatter dateFromString:curDate];`. Anyway I guess your curDate is not the same format. You could use exception handling to catch and display non converted dates. – demosten Jul 01 '13 at 12:21
  • 1
    **Don't "add a day" by adding 60x60x24x1 seconds. That is not always a day!** – David Rönnqvist Jul 01 '13 at 12:26
  • 1
    Creating a new date object and then overwriting it on the next line is completely unnecessary and would be a leak if ARC wasn't there to save you from silly mistakes. (This code: `NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:curDate];`) – David Rönnqvist Jul 01 '13 at 12:31
  • Can you print the string that is causing the crash? – David Rönnqvist Jul 01 '13 at 12:32
  • Place a breakpoint at the start of the code and single-step through it, checking values at each step. Also add the exception breakpoint as mentioned by Ramy and/or [update your `main` to dump the exception](http://stackoverflow.com/a/12268397/581994). – Hot Licks Jul 01 '13 at 15:44
  • @DavidRönnqvist - I kinda wonder if the overwritten date object isn't somehow confusing ARC and causing the crash. Overwriting a retained reference with an autoretained one could conceivably confuse ARC enough to make it do an unretain at an inopportune time. – Hot Licks Jul 01 '13 at 16:03

2 Answers2

2

You should use NSDateComponents:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString =  [dateFormatter dateFromString:self.curDate];

// Create components
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 1;

// Get the calendar to add the components to a date
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *incrementedDate = [calendar dateByAddingComponents:dateComponents toDate:dateFromString options:0];

NSString *dateDisplay = [dateFormatter stringFromDate:incrementedDate];

//set curDate to new date
curDate = dateDisplay;
rckoenes
  • 69,092
  • 8
  • 134
  • 166
-4
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"dd-MM-yyyy"];

//add a day

NSDate *dateFromString = [[NSDate date] dateByAddingTimeInterval:60*60*24*1];

//convert back to string (for use in URL string concatenation)

NSString *dateDisplay = [dateFormatter stringFromDate:dateFromString];
karthika
  • 4,085
  • 3
  • 21
  • 23
  • 2
    Did you change *any* of the OP:s code? You seem to be doing the exact same thing (including the incorrect date addition and the unused date object that would be a memory leak if ARC didn't save you). – David Rönnqvist Jul 01 '13 at 12:30
  • dateFromString = [dateFromString dateByAddingTimeInterval:60*60*24*1]; This line only getting crash. I have changed this line of code, add time in current date [NSDate date] dateFromString = [[NSDate date] dateByAddingTimeInterval:60*60*24*1]; – karthika Jul 01 '13 at 12:41
  • Then it would be very helpful to point out that change and explain why it matters. – David Rönnqvist Jul 01 '13 at 12:48
  • 4
    @karthi Since this is now the accepted answer. Can you please do something to not spread this kind of bad code. **1** Don't call `[[NSDate alloc] init];` if you never use it. **2** Don't add a fixed number of seconds and pretends that it's a day. Use `NSDateComponents *comps = [[NSDateComponents alloc] init]; comps.day = 1;` and add the date component in the correct calendar. Code like this (yeah, yeah, I know it's the OPs code) is exactly why day light saving bugs occur. – David Rönnqvist Jul 01 '13 at 14:00
  • Well you're a nice guy @HotLicks – Millie Smith Jan 16 '15 at 19:04