在for循环中添加NSDateComponents

时间:2013-03-04 14:42:58

标签: iphone ios objective-c nsdate

我试图编写一个方法来运行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;
}

1 个答案:

答案 0 :(得分: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;
}