Below there are two methods to programmatically alloc
and init
objects of various classes and 'types'.
- (id)buildObjectOfClass:(NSString *)classString andType:(NSString *)typeString
{
id buildObject;
Class className = NSClassFromString(classString);
SEL initWithTypeSelector = NSSelectorFromString(@"initWithType:");
if ([className instancesRespondToSelector:initWithTypeSelector] == YES) {
buildObject = [[className alloc] performSelector:initWithTypeSelector
withObject: typeString];
}
return buildObject;
}
This method implementation was originally written more tersely as simply:
{ return [[className alloc] initWithType:typeString]; }
My questions are: 1) is the verbose version necessary? and 2) if so, was it programmed as best as it could be? Are there shortcuts or best practices I am neglecting?