Many thanks for your replies, and for the pointers to the samples and your own
example - much appreciated. Before getting your second reply I'd just been
through the code and done something pretty similar, using the suggestions you
posted, to build up my hierarchical list for use in an NSOutlineView. Looking
at your example, I think (hope) I've now got a workable solution:
- (NSMutableDictionary *)diarySpacesForYear:(NSInteger)year
{
NSMutableArray *newDiarySpaces = [NSMutableArray array];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *yearDate, *monthDate, *dayDate;
NSRange monthRange, dayRange;
NSDateComponents *comps;
NSString *yearName = [NSString stringWithFormat:@"%i", year];
NSDictionary *localeDict = [[NSUserDefaults standardUserDefaults]
dictionaryRepresentation];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
//[dateFormatter setDateStyle:NSDateFormatterFullStyle];
//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"d - EEEE"];
// Get first day of the year.
comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:year];
yearDate = [calendar dateFromComponents:comps];
[comps release];
// Get month range.
monthRange = [calendar rangeOfUnit:NSMonthCalendarUnit
inUnit:NSYearCalendarUnit forDate:yearDate];
// Go through each month.
int month;
for (month = monthRange.location; month < NSMaxRange(monthRange);
month++)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Get first day of month.
comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:month];
[comps setYear:year];
monthDate = [calendar dateFromComponents:comps];
[comps release];
// Add this month to our array of months - get its name using
NSDateComponents.
NSString *monthName = [monthDate
descriptionWithCalendarFormat:@"%B" timeZone:timeZone locale:localeDict];
// Add this month.
NSMutableDictionary *currentMonth = [NSMutableDictionary
dictionaryWithObjectsAndKeys:
monthName, @"Title",
[NSMutableArray array], @"Days",
yearName, @"Year",
[NSNumber numberWithInt:NSMonthCalendarUnit], @"CalendarUnit",
nil];
// Now go through every day for this month.
dayRange = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit forDate:monthDate];
int day;
for (day = dayRange.location; day < NSMaxRange(dayRange); day++)
{
// Get the date.
comps = [[NSDateComponents alloc] init];
[comps setDay:day];
[comps setMonth:month];
[comps setYear:year];
dayDate = [calendar dateFromComponents:comps];
[comps release];
// Add this date to our month info.
NSMutableDictionary *day = [NSMutableDictionary
dictionaryWithObjectsAndKeys:
[dateFormatter stringFromDate:dayDate], @"Title",
[NSNumber numberWithInt:NSDayCalendarUnit], @"CalendarUnit",
nil];
[[currentMonth objectForKey:@"Days"] addObject:day];
}
// Add the month.
[newDiarySpaces addObject:currentMonth];
[pool release];
}
[dateFormatter release];
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
yearName, @"Title",
newDiarySpaces, @"Months",
[NSNumber numberWithInt:NSYearCalendarUnit],
@"CalendarUnit",
nil];
}
This gives me the same desired output, but hopefully uses the better (less
"nasty" :) ) methods.
Thanks again!
Keith
--- On Mon, 12/22/08, mmalc Crawford <[email protected]> wrote:
> From: mmalc Crawford <[email protected]>
> Subject: Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
> To: "Cocoa-Dev List" <[email protected]>
> Cc: "Keith Blount" <[email protected]>
> Date: Monday, December 22, 2008, 5:34 PM
> On Dec 22, 2008, at 8:47 AM, mmalc Crawford wrote:
>
> > There shouldn't be any need, though, to "add
> a month" for each iteration, just start a new month
> with a date components object with a new month number.
> >
> This could be made a little more efficient (and if
> you're not using garbage collection, needs suitable
> memory management), but for the sake of explicitness you
> could do something like the following.
>
> mmalc
>
>
> NSInteger specifiedYear = 2009;
>
> NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]
> init];
> [dateFormatter setDateFormat:@"EEEE d MMMM y"];
>
> NSLocale *locale = [NSLocale currentLocale];
> dateFormatter.locale = locale;
>
> NSCalendar *calendar = [locale
> objectForKey:NSLocaleCalendar];
>
> NSDateComponents *yearStartComponents = [[NSDateComponents
> alloc] init];
> [yearStartComponents setDay:1];
> [yearStartComponents setMonth:1];
> [yearStartComponents setYear:specifiedYear];
>
> NSDate *yearStartDate = [calendar
> dateFromComponents:yearStartComponents];
>
> NSRange monthsRange = [calendar
> rangeOfUnit:NSMonthCalendarUnit inUnit:NSYearCalendarUnit
> forDate:yearStartDate];
> NSUInteger months = monthsRange.length;
> NSUInteger month;
>
> for (month = 1; month <= months; month++) {
>
> NSDateComponents *monthStartComponents =
> [[NSDateComponents alloc] init];
> [monthStartComponents setDay:1];
> [monthStartComponents setMonth:month];
> [monthStartComponents setYear:specifiedYear];
>
> NSDate *monthStartDate = [calendar
> dateFromComponents:monthStartComponents];
>
> NSRange daysRange = [calendar
> rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit
> forDate:monthStartDate];
> NSUInteger days = daysRange.length;
> NSUInteger day;
>
> for (day = 1; day <= days; day++) {
>
> NSDateComponents *dayComponents =
> [[NSDateComponents alloc] init];
> [dayComponents setDay:day];
> [dayComponents setMonth:month];
> [dayComponents setYear:specifiedYear];
>
> NSDate *day = [calendar
> dateFromComponents:dayComponents];
> NSString *dayString = [dateFormatter
> stringFromDate:day];
> NSLog(@"dayString: %@", dayString);
>
> }
> }
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]