Dynamic Method Creation
Dynamic Method Creation
- Subject: Dynamic Method Creation
- From: Steve Steinitz <email@hidden>
- Date: Sat, 15 Sep 2012 14:38:06 +1000
Hello,
I'm writing a roster window for our point-of-sale app. I have some methods (on Employee) for binding to a view-based TableView (which looks a bit like a week planner), a set of methods for each day of the week. So far I have only implemented and debugged the Monday ones:
- (Shift *) mondayShift {return [self shiftForDayOfWeek: [[self class] mondayName]];}
- (void) setMondayShift: (Shift *) value_ {[self willChangeValueForKey: @"shift" withWeekdayPrefix: [[self class] mondayName]]; [self didChangeValueForKey: @"shift" withWeekdayPrefix: [[self class] mondayName]];} // KVO hack
- (NSInteger) mondayStartHourValue {return [self.mondayShift.startHour integerValue];}
- (void) setMondayStartHourValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"startHour" forWeekday: [[self class] mondayName]];}
- (NSInteger) mondayStartMinuteValue {return [self.mondayShift.startMinute integerValue];}
- (void) setMondayStartMinuteValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"startMinute" forWeekday: [[self class] mondayName]];}
- (NSInteger) mondayEndHourValue {return [self.mondayShift.endHour integerValue];}
- (void) setMondayEndHourValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"endHour" forWeekday: [[self class] mondayName]];}
- (NSInteger) mondayEndMinuteValue {return [self.mondayShift.endMinute integerValue];}
- (void) setMondayEndMinuteValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"endMinute" forWeekday: [[self class] mondayName]];}
- (NSInteger) mondayBreakDurationValue {return [self.mondayShift.breakDuration integerValue];}
- (void) setMondayBreakDurationValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"breakDuration" forWeekday: [[self class] mondayName]];}
- (Shift *) createMondayShift {return [self createShiftForDayOfWeekNamed: [[self class] mondayName]];}
- (void) clearMondayShift {[self clearShiftForWeekdayNamed: [[self class] mondayName]];}
They look prettier in Xcode - one liners.
For ease of maintenance, I would like to generate these methods dynamically, like Core Data does for its default accessors -- one set of them for each day of the week. But neither the mighty Google nor Apple's Runtime doc tells me how to do that. I was only able to learn how to "attach" an existing method to a class, not how to create methods fully programmatically - "out of thin air". I also am not able to see how to do it with macros.
Maybe I'll need some kind of third party tool - or roll my own "templating" system, like, say, mogenerator.
Thanks for any tips. And for those so inclined, feel free to tell me what a crappy idea the whole thing is, in general.
Cheers,
Steve
_______________________________________________
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