Does NSClassFromString affect performance?
Posted
by Tomen
on Stack Overflow
See other posts from Stack Overflow
or by Tomen
Published on 2010-05-26T17:13:45Z
Indexed on
2010/05/26
17:21 UTC
Read the original article
Hit count: 206
iphone
|objective-c
I want to create a controller that depends on the class of a given instance of a model
-(BaseController *)getControllerForModel:(Model *)model
{
BaseController *controller = nil;
Class controllerClass = [BaseController class]; //the default value
//find the right controller
if ([model isMemberOfClass:[ModelClass1 class]])
controllerClass = [ConcreteController1 class];
else if ([model isMemberOfClass:[ModelClass2 class]])
controllerClass = [ConcreteController2 class];
else if ([model isMemberOfClass:[ModelClass3 class]])
controllerClass = [ConcreteController3 class];
...
else if ([model isMemberOfClass:[ModelClassX class]])
controllerClass = [ConcreteControllerX class];
else
Trace(TRACELEVEL_WARNING, @"Unrecognized model type: %@", NSStringFromClass([model class]));
//Now instantiate it with the model
controller = [[[controllerClass alloc] initWithModel:model] autorelease];
return slotController;
}
I want to find a more flexible solution to this and thought of having a dictionary, which maps Model-Classes to Controller-Classes and then NSClassFromString
could give me the right instance.
My question is this: Is NSClassFromString
using much of my applications performance if i use it several times (say, 100 times at once)? Or would it be about as fast as the above approach?
© Stack Overflow or respective owner