I'm developing an iPhone app that is heavily Calendar-based, and it requires a good amount of (what I'm calling) "date boundaries."
I use these "date boundaries" to fetch EKEvents from specific calendars and display data for specific years, months, and quarters.
SPECIAL NOTE: This app is for government employees, specifically military, so the first day of their fiscal year is Oct 1st. (convenient, huh?) I can't use the calendar object to get "events from a quarter" because the fiscal year is all out of whack, so I have to do everything manually, ergo the "date boundaries."
My Question: Can these NSDateComponents objects be initialized in a loop? I'm not having performance issues, but I would prefer to have way less code to edit.
I need more than just a few of these dates, and I'm currently manually initializing all of them using NSDateComponents in my singleton class -(id)init method, like this:
-(id) init;
{
self = [super init];
if (!self) return nil;
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *refDate = [dateFormatter stringFromDate:now];
NSString *year = [refDate substringWithRange:NSMakeRange(0,4)];
NSString *month = [refDate substringWithRange:NSMakeRange(5,2)];
self.currentYear = [year integerValue];
int currentMonth = [month integerValue];
if (currentMonth <= 9) {
self.currentYear = [year integerValue] -1;
}
//init date boundaries that are used in several data model methods
// notice Q1 starts Oct, 1st
NSDateComponents *previousYearQ4EndComps = [[NSDateComponents alloc] init];
[previousYearQ4EndComps setYear:self.currentYear - 1];
[previousYearQ4EndComps setMonth:9];
[previousYearQ4EndComps setDay:30];
[previousYearQ4EndComps setHour:24];
[previousYearQ4EndComps setMinute:59];
[previousYearQ4EndComps setSecond:59];
self.previousYearQ4Ends = [cal dateFromComponents:previousYearQ4EndComps];
NSDateComponents *lastYearQ1StartComps = [[NSDateComponents alloc] init];
[lastYearQ1StartComps setYear:self.currentYear - 1];
[lastYearQ1StartComps setMonth:10];
[lastYearQ1StartComps setDay:1];
[lastYearQ1StartComps setHour:1];
[lastYearQ1StartComps setMinute:0];
[lastYearQ1StartComps setSecond:0];
self.lastYearQ1Starts = [cal dateFromComponents:lastYearQ1StartComps];
NSDateComponents *lastYearQ1EndComps = [[NSDateComponents alloc] init];
[lastYearQ1EndComps setYear:self.currentYear - 1];
[lastYearQ1EndComps setMonth:12];
[lastYearQ1EndComps setDay:31];
[lastYearQ1EndComps setHour:24];
[lastYearQ1EndComps setMinute:59];
[lastYearQ1EndComps setSecond:59];
self.lastYearQ1Ends = [cal dateFromComponents:lastYearQ1EndComps];
NSDateComponents *lastYearQ2StartComps = [[NSDateComponents alloc] init];
[lastYearQ2StartComps setYear:self.currentYear];
[lastYearQ2StartComps setMonth:1];
[lastYearQ2StartComps setDay:1];
[lastYearQ2StartComps setHour:1];
[lastYearQ2StartComps setMinute:0];
[lastYearQ2StartComps setSecond:0];
self.lastYearQ2Starts = [cal dateFromComponents:lastYearQ2StartComps];
NSDateComponents *lastYearQ2EndComps = [[NSDateComponents alloc] init];
[lastYearQ2EndComps setYear:self.currentYear];
[lastYearQ2EndComps setMonth:3];
[lastYearQ2EndComps setDay:31];
[lastYearQ2EndComps setHour:24];
[lastYearQ2EndComps setMinute:59];
[lastYearQ2EndComps setSecond:59];
self.lastYearQ2Ends = [cal dateFromComponents:lastYearQ2EndComps];
// more date boundaries...
self.eventStore = [[EKEventStore alloc] init];
self.aftpCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
self.thisYearsAFTPEvents = [[NSMutableArray alloc] init];
self.lastYearsAFTPEvents = [[NSMutableArray alloc] init];
// init the rest of my arrays...
return self;
}
And here is an example of how I'm using the NSDates I create from the NSDateComponents to retrieve events from the calendar, which works perfectly fine:
- (void)fetchAFTPEvents {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *refDate = [dateFormatter stringFromDate:now];
NSString *year = [refDate substringWithRange:NSMakeRange(0,4)];
NSString *month = [refDate substringWithRange:NSMakeRange(5,2)];
self.currentYear = [year integerValue];
int currentMonth = [month integerValue];
if (currentMonth <= 9) {
self.currentYear = [year integerValue] -1;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.aftpCalendar = [self.eventStore calendarWithIdentifier:[defaults valueForKey:@"AFTPs"]];
NSArray *calendarArray = [NSArray arrayWithObject:self.aftpCalendar];
// Fetch Last Year Q1 Events
NSPredicate *lyQ1Predicate = [self.eventStore predicateForEventsWithStartDate:self.lastYearQ1Starts
endDate:self.lastYearQ1Ends
calendars:calendarArray];
[self.lastYearsQ1AFTPEvents addObjectsFromArray:[self.eventStore eventsMatchingPredicate:lyQ1Predicate]];
[self.lastYearsAFTPEvents addObject:self.lastYearsQ1AFTPEvents];
// fetch the rest of the events...
// call delegate
[mainViewControllerDelegate updateLabels];
}
Thanks in advance for any help.