@interface multiple inheritance?

Go To StackoverFlow.com

1

I am using Objective-C in my app and I have a question about multiple inheritance in @interface declarations.

Pretty much this is what my .h looks like now:

#import "cocos2d.h"

    @interface UIViewController (Save)

    - (void)saveImage:(UIImage*)image:(NSString*)imageName;
    - (void)removeImage:(NSString*)fileName;
    - (UIImage*)loadImage:(NSString*)imageName;

    @end

    @interface CCLayer (Save)
    - (UIImage*)loadImage:(NSString*)imageName;
    - (BOOL)checkExists:(NSString*)thePath;
    @end

So as you can see, I have declared the loadImage method twice. I do not want to do this. This also means I have to have the same code in my .m twice for that method.

Is there any way to mix the UIViewController and CCLayer into 1 @interface so that I do not have to declare it multiple times?

Thanks!

2012-04-04 02:32
by SimplyKiwi
There is no multiple inheritance in Objective- - sidyll 2012-04-04 02:33
So there is absolutely no workaround for this? What if I had like 50 methods of the same thing? That is extremely tedious and unorganized - SimplyKiwi 2012-04-04 02:37


5

  1. ObjC has no multiple inheritance.
  2. you do not want multiple definitions of the same SEL in the same class.
  3. you could declare (and adopt) a protocol. then you would define the method (loadImage:) in the @interface which declared adoption. you can also declare it in a dummy category, then define loadImage: in another @implementation scope.
  4. an alternative to this is composition -- basically, you would add a class or protocol instance variable to your class and let it perform the work. if this is pubic interface, then you could also provide an accessor to it, or you could wrap it as needed. this is worth consideration if your 50 methods per @interface may be logically subdivided.
2012-04-04 02:37
by justin
Ads