例えば:
元の日付:2012年3月23日間隔:2週間ごと
出力:最新の発生->2012年6月4日
Objective-C(iOS)でこれを最善の方法で実現するにはどうすればよいですか?
補足:私の現在はそれほど機能しておらず、本当に悪い解決策です:
+ (NSDate *) getRelativeStartDateForIntervalType:(NSInteger)intervalType andStartDateTime:(NSDate *)startDateTime {
NSDateComponents *components = [[NSDateComponents alloc] init];
BOOL found = false;
NSDate *relativeStartDate;
NSDate *previousDate;
for (int i = 0; !found ; i++) {
previousDate = relativeStartDate;
switch (intervalType) {
case 0:
[components setDay:i]; // add i days
break;
case 1:
[components setWeek:i]; // add i weeks
break;
case 2:
[components setMonth:i]; // add i months
break;
case 3:
[components setDay:i]; // add i weeks
break;
default:
NSLog(@"you shouldn't be here");
break;
}
relativeStartDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:startDateTime options:0];
if ([relativeStartDate compare:[NSDate date]] == NSOrderedDescending) {
found = true;
if (i > 0 && [previousDate compare:[NSDate date]] == NSOrderedDescending) {
relativeStartDate = previousDate;
}
}
}
return relativeStartDate;
}