NSDate增量

use*_*244 0 iphone sdk xcode objective-c

我试图将nsdate增加1个月,但是当它到达年底时,年份不会更新(从第一个月开始再次)?怎么能修好?

这是我用过的代码

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease];
[comp setMonth: 1];
NSDate* newDate = [gregorian dateByAddingComponents:comp toDate:oldDate options:NSWrapCalendarComponents];
Run Code Online (Sandbox Code Playgroud)

Chu*_*uck 5

这就是NSWrapCalendarComponents选项的作用.通过0相反来获得您正在寻找的行为.