I have helper class which contain different methods. I dо not want to include it’s header file at every class that I have in my app. That approach is against DNRY I think.
Here is why I decide to create one “master” class called AppController and each class in my app to inherit it.
Here is what I do
AppController.h
#import <Foundation/Foundation.h>
#import "Helper.h"
@interface AppController : NSObject
{
ivars...
}
some methods ...
@end;
myClass.h
import "AppController.h"
@interface myClass : AppController
{
ivars...
}
some methods
@end
So far, so good.
The problem rise when some class needs to inherit not NSObject, but let’s say UIViewController. This is the moment where my AppController become useless.
I know that Objective-c is not support multiple inheritance. I have read somewhere that can be done using composition, but I can’t figure out how to do that.
I will be grateful if someone gives me some examples.
Yes, composition is how you do this. Your inheritance approach is wrong on several levels.
Here is how you approach this:
The vast majority of things people would normally use a “utility” dumping ground for are better solved with categories and functions in ObjC. The remainder are usually best served using delegation (but that’s another topic).