开发者

representing time interval in objective-C

I know that there is a data type called NSTimeInterval, but this is in seconds. I want to have an object representation that would be able to represent a time 开发者_运维百科range, say Thursday 21 June 8:00 - Thursday 21 June 9:00. Later on I want to compare the current date/time and check whether it fits within this range. What is the best way to do this?


I would suggest using two NSDate objects to store the start and end dates. You can easily determine if a date is between them using the timeIntervalSinceDate: method:

- (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);
}

You just need to make a class which holds two NSDate objects, making sure the first is before the second, and including this method.

FYI, NSTimeInterval is not a class, its a typedef of double.

Edit

Since you want use these as keys for a dictionary, you could use something similar to this to store and search your data:

@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

Usage:

Example interval class:

@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

Example usage code:

//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


NSDateComponents can be used to store components of time intervals as well as date components. You can add such an object to an NSDate using NSCalendar's dateByAddingComponents:toDate:options:.


From iOS10+ you have NSDateInterval (See https://developer.apple.com/documentation/foundation/nsdateinterval?language=objc). Is the best data structure to represent a time interval.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜