0

我正在尝试将 NSString 转换为日期,添加一天,然后转换回字符串。

到目前为止,我的代码是这样的:

//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;

它在以下位置崩溃到堆栈:

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

(不确定这是否有用)。

谁能说这是为什么?

谢谢!

4

2 回答 2

2

你应该使用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;
于 2013-07-01T15:01:32.600 回答
-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];
于 2013-07-01T12:27:29.217 回答