Working with weekdays on NSDate, NSDateComponents, NSCalendar and NSValueTransformer
Working with weekdays on NSDate, NSDateComponents, NSCalendar and NSValueTransformer
- Subject: Working with weekdays on NSDate, NSDateComponents, NSCalendar and NSValueTransformer
- From: Michele Barboni <email@hidden>
- Date: Mon, 16 Mar 2009 20:40:24 +0100
Hi,
I'm developing a small CoreData app which is essentially a week
scheduler, so, I " don't have " to worry about years, month, days
etc.. only weekdays, hours and minutes.
For editing timings (weekday, hour and minute) I have a
NSSegmentedControl with 7 segments named Monday, Thuesday, Wednesday
and so on and a NSDatePicker for hour and minute.
Both the two NSSegmentedControl (selectedTag) and NSDatePicker (value)
are binded to the Date attribute of my entity.
While NSDatePicker of course doesn't need a value transformer, for the
NSSegmentedControl I've coded a value transformer:
- (id)transformedValue:(id)value
{
// input: NSDate, output: int
if(value = nil) return nil;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekDayComponents = [gregorian
components:NSWeekdayCalendarUnit fromDate:value];
int weekday = [weekDayComponents weekday];
[gregorian release];
[weekDayComponents release];
return [NSNumber numberWithInt:weekday];
}
- (id)reverseTransformedValue:(id)value
{
// input: int, output: NSDate
if(value = nil) return nil;
NSDate *d = [sedutaTime dateValue];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:
(NSWeekdayCalendarUnit) fromDate:d];
[comps setWeekday:(NSInteger)value];
d = [gregorian dateByAddingComponents:comps toDate:d options:0];
[gregorian release];
[comps release];
return d;
}
sedutaTime is the NSDatePicker.
transformedValue seems to work but the reverse simply set default
dates and nothing more, seems like only the setWeekday: isn't enough..
Anyone had to deal with this kind of scenario?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden