I have two classes that are already subclasses of a different parent (due to an external library I cannot change this). However I want to share some common code between them, say for example, code that handles an action after a popup dialog etc. How do I do this?
views:
59answers:
3
+1
A:
You can re-factor the appropriate code into a utilities class, and then have the two classes call it. As for the iPhoneSDK, you can probably have the utility method be the delegate method itself.
notnoop
2009-11-13 19:30:42
+1
A:
You could write a category on a common ancestor class. Then both classes could import that Category and call the common code.
Patrick Burleson
2009-11-13 19:33:41
I'd be concerned about this - the OP specified that the classes don't have the same parent, so the only "common ancestor" they may have could be NSObject. I wouldn't want to clutter NSObject with specific categories for something like this.
Tim
2009-11-13 19:49:56
I agree that putting it on NSObject would potentially be cluttering things, but it is a valid way to do what the OP wants to do without holding an instance of some utility class.
Patrick Burleson
2009-11-13 20:01:09
Does this method allow the common code to have access to their private variables as if the code is part of a superclass?
erotsppa
2009-11-13 20:24:55
+4
A:
Refactor the shared code into its own class, and include an instance of that class as a field/property within your other two classes.
David Lively
2009-11-13 19:33:56