1

私はiPhone開発者の初心者ですが、

追加したいcustom dateカスタムの日付に、または1 month頻度に応じて作成しました。3 month6 month

newDate が今日の日付よりも小さくなるまで月を追加したい。

これが私のコードスニペットです。

 while ([today compare:temp] == NSOrderedDescending) 
    {        
        NSLog(@"inside----- today=%@,temp=%@",today,temp);

        //add to dates
        NSDateComponents *newComponents= [[NSDateComponents alloc] init];

        if([FrequencySelText isEqualToString:@"Monthly"]){

            [newComponents setMonth:1];
        }
        if([FrequencySelText isEqualToString:@"Quarterly"]){

            [newComponents setMonth:3];
        }
        if([FrequencySelText isEqualToString:@"Half - Yearly"]){

            [newComponents setMonth:6];
        }
        if([FrequencySelText isEqualToString:@"Yearly"]){

            [newComponents setMonth:12];
        }

        // get a new date by adding components
        NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd/MM/yyyy"];

        NSString *newDate = [formatter stringFromDate:temp];
        NSLog(@"new date=%@",newDate);
}

私のログは次のとおりです。 inside----- today=2012-07-11 14:41:27 +0000,temp=2012-04-12 03:00:00 +0000

私はExc_bad_accessこの行に来ています、NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];

どんな助けでも大歓迎です。

4

2 に答える 2

3

temp に再度値を指定すると、NSDate オブジェクト タイプを再度指定する必要がなくなります。ここですでに NSDate *temp を持っているのに、NSDate オブジェクトに同じ名前を再び使用する理由

 temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0] 
于 2012-07-11T06:58:49.007 に答える
0

多分これを試してください:

NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

[dateComponents setMonth:1]; // Sample Add   

NSDate *tempDate = [gregorian dateByAddingComponents:dateComponents toDate: temp options:0];
NSString *strTempDate = [dateFormat stringFromDate:tempDate];

// New Date on strTempDate
于 2012-07-11T08:43:00.227 に答える