objective c - Forwarding call to foreign -init method -
situation
for project of mine, i'm building kind of extension. extension must have class implements method declaration - (id)initwithbundle:(nsbundle *)bundle.
issue
my extension has multiple classes, host app badly written calls - (id)initwithbundle:(nsbundle *)bundle on different classes, randomly.
i'm not willing reduce number of classes, solution left somehow forward caller class implement - (id)initwithbundle:(nsbundle *)bundle (a bit http 302). found many resources on forwarding calls, not such thing forwarding -init method...
init allowed return object other itself. while highly recommend fixing calling code (i can't imagine case allowing code calls "randomly" reasonable idea), if want return other object init, works this:
- (id)initwithbundle:(nsbundle *)bundle { // don't implement this, let's return class return [[otherclass alloc] initwithbundle: bundle]; } arc deal throwing away.
the caller has wrong type of course (they expect type, , have other random object), lead hijinks, hard track bugs, , general sorrow. it's legal objc. , if 2 classes have enough overlap of methods, might work.
in normal cases, pattern called class cluster. see what called "class cluster" in objective-c? examples.
Comments
Post a Comment