Re: Dynamic Method Creation
Re: Dynamic Method Creation
- Subject: Re: Dynamic Method Creation
- From: Jean Suisse <email@hidden>
- Date: Sat, 15 Sep 2012 19:59:31 +0200
Yes it is that simple !
I have been using it to synthesize a whole set of hardware interaction methods following a pattern. The more methods you have, the more time you save. Also, you can modify the whole code just by editing it in one place.
Some (like Matt Gallagher and his SYNTHESIZE_SINGLETON macro) have even been using this technique to synthesize whole class and their methods.
There are a few drawbacks thought. It's best to thoroughly test synthesized methods. Because they are harder to debug.
Since the code is located in a macro, you don't have syntax coloring, and sometimes, autocompletion may not work.
Cheers,
Jean
On 15 sept. 2012, at 11:11, Steve Steinitz wrote:
> Hi Jean,
>
> On 15 Sep 12, at 6:12pm, Jean Suisse wrote:
>
>> There seem to be a pattern in you methods: they all contain the name of a day of the week (monday) and you want them for all days.
>
> Yes, that was intentional, just for this purpose :)
>
>> Then you could use macros.
>>
>> Example :
>>
>> #define SYTNHESIZE_WHATEVER_BUNCH_OF_FUNCTION_FOR_DAY(dayName) \
>> - (Shift *) dayName##CreateShift { return [self createShiftForDayOfWeekNamed:[[self class] dayName##Name]];}
>> - (void) dayName##ClearShift {[self clearShiftForWeekdayNamed:[[self class] dayName##Name]];}
>
>> Then, in your code, you will just need :
>> SYTNHESIZE_WHATEVER_BUNCH_OF_FUNCTION_FOR_DAY(monday);
>> SYTNHESIZE_WHATEVER_BUNCH_OF_FUNCTION_FOR_DAY(tuesday);
>> etc.
>
> Wow! Is it really that simple? Thank you Jean, you have saved the day.
>
>> You will probably want two sets of macro :
>> DECLARE_WHATEVER_BUNCH_OF_FUNCTION_FOR_DAY(dayName)
>> SYTNHESIZE_WHATEVER_BUNCH_OF_FUNCTION_FOR_DAY(dayName)
>
> Right, one for the .h and one for the .m. Good point.
>
> Thanks again, Jean.
>
> All the best,
>
> Steve
>
Jean Suisse
Institut de Chimie Moléculaire de l’Université de Bourgogne
(ICMUB) — UMR 6302
U.F.R. Sciences et Techniques, Bâtiment Mirande
Aile B, bureau 411
9, avenue Alain Savary — B.P. 47870
21078 DIJON CEDEX
_______________________________________________
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