首先,檢查一個NSDate是否在NSTimeInterval中是沒有意義的,因爲NSTimeInterval只是指定了一段時間,而不是它的位置。相反,您希望使用兩個單獨的NSDate來指定間隔的開始和結束。
下面是它的樣子(開始時間和結束時間是NSDates)。
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:yourContext];
NSPredicate *beginningPredicate = [NSPredicate predicateWithFormat:@"createdAt >= %@", beginningTime];
NSPredicate *endPredicate = [NSPredicate predicateWithFormat:@"createdAt <= %@", endTime];
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:beginningPredicate, endPredicate, nil]];
NSArray *results = [yourContext executeFetchRequest:request error:NULL];