(newStartDate, newEndDate)
(startDate, endDate)
与if重叠
(newStartDate <= endDate) && (startDate <= newEndDate)
(这几乎是@Bergasms 的建议,但不完全是。如果我在这里弄错了,应该归功于他。)
您可以使用以下获取请求来检查重叠课程:
NSDate *newStartDate = ...;
NSDate *newEndDate = ...;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SIMCourse"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ <= endDate) AND (startDate <= %@)",
newStartDate, newEndDate];
request.predicate = predicate;
NSArray *results = [context executeFetchRequest:request error:&error];
if (error == nil) {
// handle error
} else if (results.count == 0) {
// no overlapping entries
} else {
//overlapping entries in results array
}
我在这里假设如果一门课程的结束日期等于另一门课程的开始日期,则两门课程重叠。如果此类间隔不被视为“重叠”,则可以在谓词中替换<=
为。<