Reputation: 6490
I am new to iPhone developer,
I made a one custom date
in that custom date i want to add 1 month
or 3 month
or 6 month
according to frequency.
i want to add month till newDate is less then today's date.
here is my code snippet,
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);
}
My Log shows: inside----- today=2012-07-11 14:41:27 +0000,temp=2012-04-12 03:00:00 +0000
I am getting Exc_bad_access
at this line, NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];
Any help will bw appreciated.
Upvotes: 1
Views: 152
Reputation: 5242
Maybe try this one:
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
Upvotes: 0
Reputation: 38249
Providing value to temp again then no need provide NSDate object type again. Here u already have NSDate *temp then why r u using same name again for NSDate object
temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0]
Upvotes: 3