ios - Objective-C return subclass based on Xcode project target? -
i have xcode project compile 2 targets, sake of simplicity we'll call them 'foo' , 'bar'. have superclass , 2 subclasses, how can cleanly ensure variable returned different subclass based on project target?
so example, have following...
viewmanager.h //super class fooviewmanager.h //foo subclass barviewmanager.h //bar subclass ... viewmanager * viewmanager; #if target_foo viewmanager = [[fooviewmanager alloc] init]; #elif target_bar viewmanager = [[barviewmanager alloc] init]; #else viewmanager = [[viewmanager alloc] init]; #endif ...
is there way can make code cleaner, in caller class dont need compiler #if
statements, , have initialize superclass, , maybe superclass init method can take care of switching based on current project target, , return correct subclass within viewmanager
variable?
i'm sure there's standard pattern doing in objective-c i'm missing, appreciated!
it possible locate required subclass definition in 1 place, i.e., make define once , use on places:
#if defined(target_foo) #define currentviewmanager fooviewmanager #elif defined(target_bar) #define currentviewmanager barviewmanager #else #define currentviewmanager viewmanager #endif
and use new define everywhere without preprocessor macros:
currentviewmanager *manager = [[currentviewmanager alloc] init];
probably find useful incapsulate logic in fabric method:
+ (viewmanager *)createviewmanager { // preprocessor switches here // ... return [[currentviewmanager alloc] init]; }
Comments
Post a Comment