表示目标-C中的时间间隔

时间:2011-03-27 02:02:43

标签: iphone objective-c

我知道有一种名为NSTimeInterval的数据类型,但这只是几秒钟。我希望有一个能够代表时间范围的对象表示,例如6月21日星期四8:00 - 6月21日星期四9:00。后来我想比较当前的日期/时间并检查它是否适合这个范围。这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:3)

我建议使用两个NSDate对象来存储开始和结束日期。您可以使用timeIntervalSinceDate:方法轻松确定日期之间是否存在日期:

- (BOOL)dateInInterval:(NSDate *)testDate {
    // date1 is the instance variable containing the starting date
    // date2 is the instance variable containing the ending date
    return ([testDate timeIntervalSinceDate:date1] > 0 &&
            [testDate timeIntervalSinceDate:date2] < 0);
}

你只需要创建一个包含两个NSDate对象的类,确保第一个在第二个之前,并包括这个方法。

仅供参考,NSTimeInterval不是一个类,它的typedef为double。

修改

由于您希望将这些用作词典的键,因此您可以使用与此类似的内容来存储和搜索数据:

@protocol IntervalDictionaryKey <NSObject>
// The class you use as keys for your dictionary must implement this method to determine if a object is in the interval
- (BOOL)intervalContains:(id)object;
@end
@interface IntervalDictionary : NSObject {
    NSMutableArray *keys, *values;
}
- (void)addInterval:(id<IntervalDictionaryKey>)interval withObject:(id)object;
- (void)setObject:(id)object forIntervalOf:(id)intervalObject;
- (id)objectForIntervalOf:(id)object;
@end


@implementation IntervalDictionary
- (id)init {
    if(self = [super init]) {
        keys = [NSMutableArray new];
        values = [NSMutableArray new];
    }
    return self;
}
- (void)dealloc {
    [keys release];
    [values release];
    [super dealloc];
}
- (void)addInterval:(id<IntervalDictionaryKey>)interval withObject:(id)object {
    [keys addObject:interval];
    [values addObject:object];
}
- (void)setObject:(id)object forIntervalOf:(id)intervalObject {
    id<IntervalDictionaryKey> key;
    NSUInteger i = 0;
    for(key in keys) {
        if([key intervalContains:intervalObject]) {
            [values replaceObjectAtIndex:i withObject:object];
            break;
        }
        ++i;
    }
}
- (id)objectForIntervalOf:(id)object {
    id<IntervalDictionaryKey> key;
    NSUInteger i = 0;
    for(key in keys) {
        if([key intervalContains:object]) {
            return [values objectAtIndex:i];
        }
        ++i;
    }
}
@end

用法:

示例间隔类:

@interface DateInterval : NSObject <IntervalDictionaryKey> {
    NSDate *date1, *date2;
}
- (BOOL)intervalContains:(NSDate *)date; // this is the same as the dateInInterval method above
@end
@implementation DateInterval
// initializer which sets date1 and date2
- (BOOL)intervalContains:(NSDate *)date {
    return ([date timeIntervalSinceDate:date1] > 0 &&
            [date timeIntervalSinceDate:date2] < 0);
}
@end

使用代码示例:

//intervalX is a DateInterval object, created elsewhere
//objectX is any object, created elsewhere
//objectInX is a NSDate which is part of intervalX, created elsewhere
IntervalDictionary *dict = [IntervalDictionary new];
[dict addInterval:interval0 withObject:object0];
[dict addInterval:interval1 withObject:object1];
[dict objectForIntervalOf:objectIn0]; // returns object0
[dict objectForIntervalOf:objectIn1]; // returns object1
[dict setObject:object2 forIntervalOf:objectIn1]; // changes the object for interval1 to object2
[dict objectForIntervalOf:objectIn1]; // now returns object2

答案 1 :(得分:0)

NSDateComponents可用于存储时间间隔的组件以及日期组件。您可以使用NSCalendar的dateByAddingComponents:toDate:options:将此类对象添加到NSDate。

答案 2 :(得分:0)

在iOS10以上版本中,您具有 NSDateInterval (请参见https://developer.apple.com/documentation/foundation/nsdateinterval?language=objc)。 是表示时间间隔的最佳数据结构。