0

我正在尝试编写一个方法,该方法将运行 for 循环检查日期是否在今天之前,如果是,则需要将其增加 NSDateComponent

检查日期是否早于今天的方法:

- (BOOL) isDatePassedToday:(NSDate *)recurringDate
{
    if ([recurringDate compare:[NSDate date]] == NSOrderedDescending)
        return NO;

    return YES;
}

我需要我的 for 循环如何工作的逻辑:

-(NSDate *)calculateRecurringReminder:(NSDate *)startDate using:(NSDateComponents*)doItAgainComponents
{
    NSDate *recurringDate = [[NSDate alloc]init];

    recurringDate = [[NSCalendar currentCalendar] dateByAddingComponents:doItAgainComponents toDate:recurringDate options:0];

    // The loop to add components
    for ([self isDatePassedToday:recurringDate]; [self isDatePassedToday:recurringDate] == YES; doItAgainComponents)
    {

        recurringDate = [[NSCalendar currentCalendar] dateByAddingComponents:doItAgainComponents toDate:recurringDate options:0];

    }

    return recurringDate;
}
4

1 回答 1

0

也许你的意思是:

-(NSDate *)calculateRecurringReminder:(NSDate *)startDate using:(NSDateComponents*)doItAgainComponents {
    NSDate *recurringDate = startDate;

    while (![self isDatePassedToday:recurringDate]) {
        recurringDate = [[NSCalendar currentCalendar] dateByAddingComponents:doItAgainComponents toDate:recurringDate options:0];
    }

    return recurringDate;
}
于 2013-03-04T14:51:38.507 回答