UPDATE:
With some key suggestions, back and forth with George, I came up with two different ways to achieve exactly what I want in CodeRunner, and posted it on the Github gist website: Objective-C AOP gist
The code is rude because it is a new concept, and I just finished at 1:30 in the morning. This definitely works, although it has some subtleties, such as automatically adding all methods that are not initializers, getters, or setters. [END UPDATE]
Several times (but, of course, not very often), I came across a situation where my code would be a bit DRYer if I could name the context-sensitive part of the code for each method in the class. Using Objective-C runtime is completely fine, I would make C or C ++ decisions.
Instead:
- (void)methodName1 { self->selector = _cmd; NSLog(@"This method is named: %@",_cmd);
There is something like this, with the same result:
+ (void)AOPMethod { self->selector = _cmd; NSLog(@"This method is named: %@",_cmd); } - (void)methodName1 {
In a real application, AOPMethod will contain more code, and there will be more methods in the class.
PS, I'm pretty obsessed with DRY. Along with clarity of prose and performance, this is a key component of how I evaluate the quality of my code in the long run. For each new method, I can avoid repetition, the advantage is exponential, because I break as much code as possible in the reusable classes that are used in many projects.
c ++ c objective-c aop dry
james_womack
source share