我正在創建自定義類型日曆,並試圖查看是否可以將日期存儲在數組中,而不會靜態分配每個日期。例如,數組中的第一個日期應該是它第一次創建的那一天,並且可以將下一週保存到數組中的相關索引中。將當前和未來日期添加到數組
NSMutableArray *thisWeek = [today, tomorrow, sunday(Feb 24), monday (Feb 25), etc];
什麼是最好的方式去存儲未來的日期?
我正在創建自定義類型日曆,並試圖查看是否可以將日期存儲在數組中,而不會靜態分配每個日期。例如,數組中的第一個日期應該是它第一次創建的那一天,並且可以將下一週保存到數組中的相關索引中。將當前和未來日期添加到數組
NSMutableArray *thisWeek = [today, tomorrow, sunday(Feb 24), monday (Feb 25), etc];
什麼是最好的方式去存儲未來的日期?
NSMutableArray *days = [[NSMutableArray alloc] init];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *tempCop = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:tempCop];
for (int i = 0; i < 8; i++)
{
NSDateComponents *comps = [[NSDateComponents alloc]init];
[comps setDay:i];
[days addObject:[cal dateByAddingComponents:comps toDate:today options:0]];
}
看一看dateByAddingTimeInterval:
的NSDate
文檔(link)。它可以讓你添加一個給定的秒數到一個日期。
NSMutableArray *days;
days = [[NSMutableArray alloc] init];
NSDate *todayDate = [NSDate Date];
[days addObject:todayDate];
for (int i = 1; i <= 6; i++)
{
NSDate *newDate = [[NSDate date] dateByAddingTimeInterval:60*60*24*i];
[days addObject:newDate];
}
以這種方式,您可以在數組中添加天數。
沒有得到您的問題。但是,您可以將日期存儲在數組中,因此沒有任何問題。 – 2013-02-22 17:08:45
我的問題是,我如何在第一次約會時將數週的日期存儲在數組中? – bardockyo 2013-02-22 17:11:19