-1
我有這樣的方法刪除字典中的所有對象,如果新的一天又開始了:混淆了NSDateComponents
-(void)flushDictionaryIfNeeded{
NSLog(@"Flush called");
if([self.messagedTodayDict count]>0) {
//get any date
NSDate *aDate = nil;
NSArray *values = [self.messagedTodayDict allValues];
aDate = [values objectAtIndex:0];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:aDate];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
NSLog(@"Today is %@",[NSDate date]);
NSLog(@"Date in the dicitonary is %@",aDate);
if([today day] == [otherDay day] &&
[today month] == [otherDay month] &&
[today year] == [otherDay year] &&
[today era] == [otherDay era]) {
NSLog(@"Don't flush");
}
else {
NSLog(@"It's a new day! erase dictionary!");
[self.messagedTodayDict removeAllObjects];
}
}
}
我住在臺灣的NSLog爲我打印任何日期是在時間後8小時這裏。我把這種方法5分鐘午夜前(臺灣時間)我得到這樣的:存儲8小時的背後
Today is 2015-04-03 15:55:09 +0000
Date in the dicitonary is 2015-04-03 15:09:09 +0000
這是有道理的,日期和字典不沖水/刪除所有對象。
現在我調用該方法在午夜:
Today is 2015-04-03 16:00:22 +0000
Date in the dicitonary is 2015-04-03 15:09:09 +0000
和字典中刪除的所有對象。爲什麼如果我在比較下面的日期組件呢?今天的日期和日期與字典中的日期相同。真的不明白這裏發生了什麼。一些指針將非常感激。
非常感謝。此方法偶爾會刪除字典中的所有元素(當它尚未過午夜時),以便了解日期如何存儲和顯示。謝謝 – Kex 2015-04-03 16:24:07