Optimal way to initialize varying objects

Posted by John Smith on Stack Overflow See other posts from Stack Overflow or by John Smith
Published on 2010-04-24T22:38:30Z Indexed on 2010/04/24 22:43 UTC
Read the original article Hit count: 152

Filed under:
|
|
|
|

I have to initialize a lot of different types of objects based on an integer parameter. They all have the same overall initialization methods.

At the moment I have the following code

#def APPLE 1
#def PEAR 2
switch (t)
{
    case APPLE:
      newobj = [[FApple alloc] init];
      break;
    case PEAR:
      newobj = [[FPear] alloc] init];
      break;
    default:
      retobj = nil;
}

I believe there must be a better way to do this. When I add FOrange I have to go and add another line here.

What would be a better way?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa