Iphone 在for循环中添加NSDateComponents
我正在尝试编写一个方法,该方法将运行for循环检查日期是否早于今天,如果早于今天,则需要通过NSDateComponent递增 检查日期是否早于今天的方法:Iphone 在for循环中添加NSDateComponents,iphone,ios,objective-c,nsdate,Iphone,Ios,Objective C,Nsdate,我正在尝试编写一个方法,该方法将运行for循环检查日期是否早于今天,如果早于今天,则需要通过NSDateComponent递增 检查日期是否早于今天的方法: - (BOOL) isDatePassedToday:(NSDate *)recurringDate { if ([recurringDate compare:[NSDate date]] == NSOrderedDescending) return NO; return YES; } -(NSDate
- (BOOL) isDatePassedToday:(NSDate *)recurringDate
{
if ([recurringDate compare:[NSDate date]] == NSOrderedDescending)
return NO;
return YES;
}
-(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;
}
我需要for循环如何工作的逻辑:
- (BOOL) isDatePassedToday:(NSDate *)recurringDate
{
if ([recurringDate compare:[NSDate date]] == NSOrderedDescending)
return NO;
return YES;
}
-(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;
}
也许你的意思是:
-(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;
}
那么你对我们的期望是什么?那么你对循环的期望行为是什么?它实际上做了什么?我只是希望循环不断地将doItAgainComponents添加到recurringDate中,直到它今天通过为止。嗯,虽然这似乎正是我所需要的,但它锁定了我的UI,即使是很小的计算,似乎也无法完成。有什么建议吗?也许这是一个无限循环。在调试器中运行此命令并查看原因。也许
recurringDate
没有按预期递增,或者isDatePassedToday
永远不会返回YES
。对不起,我想得不清楚,是的,你是对的,它正在进入一个无限循环。我想我知道这个问题,但是我原来的问题得到了回答,所以非常感谢。