objective c - About moving few methods to the superclass -
objective c - About moving few methods to the superclass -
i need move same method 4 different classes superclass. such methods same except type of variable declared in them:
for example, in method in first class have
firstclass var = [[firstclass alloc] init]
in sec class
secondclass var = [[secondclass alloc] init]
and on.
what's best way implement variation in superclass ?
should utilize nsclassfromstring in superclass , each string each method in subclasses?
thanks
if different types of ivar's intend on initializing in subclasses descended mutual class, i'd store class in super, or else store id. then, setup property accessor in each of subclasses casts ivar need it.
@interface superclass : nsobject{ id _superivar; } @end @implementation superclass : nsobject ....super's code.... @end
now in implementation of subclass declare property in category, shown below (or in interface, if want public)
@interface subclass (private) @property (strong) classtype *superivar; @end; @implementation - (void) setsuperivar:(classtype *)superivar{ _superivar = superivar; } - (classtype *) superivar{ homecoming (classtype *) _superivar; } - (void) somemethodthatusessuperivar{ [self.superivar dosomething]; } @end
alternatively, if don't want open _superivar direct access, can set property on superclass , access through property on subclass. in way can access super's ivars cast appropriate type.
objective-c cocoa
Comments
Post a Comment