2011-02-16 3 views
3

일광 절약 시간, 윤년 등을 고려한 30 일 배열을 만들려고합니다. 현재 배열을 만드는 생성기가 있지만 특별한 시간 변경을 고려하지 않으며 년, 월이 바뀝니다. 여기에 내 현재 코드입니다 :NSCalendar 또는 NSDate를 사용하여 30 일을 배열로 생성하는 방법은 무엇입니까?

오늘의 날짜와 다음 이십구일을 포함해야 오늘의 날짜와 배열부터 향후 30 일을 검색하는 달력을 통해 루프 발전기를 만들 수있는 가장 좋은 방법은 무엇
NSMutableArray* dates = [[NSMutableArray alloc] init]; 
    int numberOfDays=30; 
    NSDate *startDate=[NSDate date]; 
    NSDate *tempDate=[startDate copy]; 
    for (int i=0;i<numberOfDays;i++) { 
     NSLog(@"%@",tempDate.description); 
     tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)]; 
     [dates addObject:tempDate.description]; 
    } 

    NSLog(@"%@",dates); 

. 내 현재 코드는 내가 말한 것처럼 작동하지만 완전히 정확하지는 않습니다. 감사합니다.

답변

9

거의 다 알았습니다. 몇 가지 수정 :

int numberOfDays=30; 

NSDate *startDate=[NSDate date]; 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *offset = [[NSDateComponents alloc] init]; 
NSMutableArray* dates = [NSMutableArray arrayWithObject:startDate]; 

for (int i = 1; i < numberOfDays; i++) { 
    [offset setDay:i]; 
    NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0]; 
    [dates addObject:nextDay]; 
} 
[offset release]; 

NSLog(@"%@",dates); 

이렇게하면 NSDate 개의 배열이 생성됩니다. 내 컴퓨터에,이 기록합니다 시간대는 -0800에서 -0700 3 월 13 일에 변화를 상쇄하는 방법

EmptyFoundation[4302:903] (
    "2011-02-16 16:16:26 -0800", 
    "2011-02-17 16:16:26 -0800", 
    "2011-02-18 16:16:26 -0800", 
    "2011-02-19 16:16:26 -0800", 
    "2011-02-20 16:16:26 -0800", 
    "2011-02-21 16:16:26 -0800", 
    "2011-02-22 16:16:26 -0800", 
    "2011-02-23 16:16:26 -0800", 
    "2011-02-24 16:16:26 -0800", 
    "2011-02-25 16:16:26 -0800", 
    "2011-02-26 16:16:26 -0800", 
    "2011-02-27 16:16:26 -0800", 
    "2011-02-28 16:16:26 -0800", 
    "2011-03-01 16:16:26 -0800", 
    "2011-03-02 16:16:26 -0800", 
    "2011-03-03 16:16:26 -0800", 
    "2011-03-04 16:16:26 -0800", 
    "2011-03-05 16:16:26 -0800", 
    "2011-03-06 16:16:26 -0800", 
    "2011-03-07 16:16:26 -0800", 
    "2011-03-08 16:16:26 -0800", 
    "2011-03-09 16:16:26 -0800", 
    "2011-03-10 16:16:26 -0800", 
    "2011-03-11 16:16:26 -0800", 
    "2011-03-12 16:16:26 -0800", 
    "2011-03-13 16:16:26 -0700", 
    "2011-03-14 16:16:26 -0700", 
    "2011-03-15 16:16:26 -0700", 
    "2011-03-16 16:16:26 -0700", 
    "2011-03-17 16:16:26 -0700" 
) 

참고. 그것은 일광 절약 시간입니다. :)

+0

단지 (!) 참고 : 나는 한 달 대신 하드 코딩 (30) :-) 실제로 일을 얻을 것입니다 (나는 그가 요구 뭔지). –

+0

@Rasmus 확실합니다. :) –

+0

코드에 대한 Dave의 감사와 sidenote에 대한 감사 Rasmus. 생성기에 코드 rasmus를 어떻게 구현합니까? – 0SX

1

위 내 (!) 참고를위한 몇 가지 코드 :

- (NSRange) daysInMonth:(NSDate*)date { 

    NSCalendar* cal = [NSCalendar currentCalendar]; 
    NSDateComponents *comps = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit) 
            fromDate:(date != nil) ? date: self.currentMonth]; 

    NSRange range = [cal rangeOfUnit:NSDayCalendarUnit 
           inUnit:NSMonthCalendarUnit 
          forDate:[cal dateFromComponents:comps]]; 

    return range; 
} 
+0

그럼이 코드를 어떻게 부르겠습니까? int numberOfDays = ??? – 0SX

+0

NSRange \t daysInMonth = [자체 일수] [NSDate 날짜]]; int numberOfDays = daysInMonth.length; –

+0

+1 Thx 그러나 내 viewcontroller가 -daysInMonth에 응답하지 않을 수 있으며 길이가 구조체 또는 공용체가 아니라고합니다. – 0SX

관련 문제