2011-03-27 60 views
2

我知道有一種數據類型叫NSTimeInterval,但這是以秒爲單位的。我想要一個能夠表示時間範圍的對象表示,例如6月21日星期四8:00至6月21日星期四9:00。稍後,我想比較當前的日期/時間並檢查它是否適合此範圍。做這個的最好方式是什麼?代表目標中的時間間隔-C

回答

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不是類,它是double的typedef。

編輯

既然你想利用這些作爲一個字典,你可以使用類似這樣的東西來存儲和檢索數據的密鑰:

@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 
+0

如果日期總是相同的日期,它會改變你的答案嗎? – aherlambang 2011-03-27 02:27:46

+0

然後,您可以只保存參考日期的偏移量,但它在使用中不會有太大的區別。 – ughoavgfhw 2011-03-27 02:30:47

+0

問題是開始日期和結束日期正在變化 – aherlambang 2011-03-30 04:11:48