I'm having a bit of a brain fart. I hope someone can help me.
I have an app that communicates with different devices. Based on which device it's communicating with, I need to use different methods.
I have several different classes that do some parsing that I have made subclasses of a class called Parse
(Don't confuse with the Parse cloud service), these only have Class methods. e.g. Parse_Device1
Parse_Device2
I have a third class (lets call it Selection
) that returns all sorts of different stuff depending on what device the app is communicating with.
How can I have this class return the needed Parse
subclass and use what's returned?
I know I could easily alloc
and init
the needed class, store it or return it.
However as all the methods on the parse classes are just Class methods, there's no need to init
at all.
I've tried like this, however I don't seem to be able to use the returned class directly:
+ (Parser *)parser_AccessList
{
switch ([self brand]) {
case Brand1:
return [Parse_AccessList_1 alloc];
break;
case Brand2:
return [Parse_AccessList_2 alloc];
break;
case Brand3:
return [Parse_AccessList_3 alloc];
break;
default:
return nil;
break;
}
}
I'm probably missing something really simple here.
Thanks
This returns the class object. The caller may then instanciate on object of that very class or call class methods to it (by sending class method selectors).
return [Parse_AccessList_1 class];
Let's assume this is within the caller somewhere:
Class clazz = yourObject parser_AccessList];
[clazz performSelector:@selector(someMethod)];
As clazz
is an object of type Class
the related class method someMethod
shoulc be called.
However, I am sure there are better ways of reaching your goal. Try using a delegation pattern and/or dependency inversion.