One of the awkward decisions is to create an abstract superclass that gives you a normal synthesis of properties. Then create a specific subclass that you are actually using and that simply implements and overrides the method (same signature) and calls super to do the actual setup. This allows you to do what you want to do before or after calling the super implementation.
Example:
@interface ALTOClassA : NSObject @property NSString *catName; @end
Nothing more is needed in the .m outside the stub file for this test.
Create a subclass, nothing special is needed in @interface
#import "ALTOClassA.h" @interface ALTOClassAJunior : ALTOClassA @end
At @implementation, we do our redefinition.
#import "ALTOClassAJunior.h" @implementation ALTOClassAJunior - (void)setCatName:(NSString*)aCatName { NSLog(@"%@",NSStringFromSelector(_cmd)); [super setCatName:aCatName]; NSLog(@"after super: self.catName %@", self.catName); } @end
Using:
ALTOClassAJunior *aCAJ = [ALTOClassAJunior new]; NSLog(@"aCAS.catName %@", aCAJ.catName); NSLog(@"set it to George."); [aCAJ setCatName:@"George"]; NSLog(@"aCAS.catName %@", aCAJ.catName);
This allows you to use auto-generated code and still do what you want to do with your class. Abstract A superclass is often a useful solution for many things.
uchuugaka
source share