0

I have a class "Projectiles", and I want to create an object with it. To minimise code, I want to specify the object from a string, it would clean up thins alot.

Example: I have the string,

tempenemy.atktype = @"homing_fireball";

Now i want to create an object with the same name from Projectiles class:

Projectiles *tempenemy.atktype;

Is this possible? So the final result would be an object from Projectiles class called homing_fireball..?

Thanks!!

Whimsical
  • 5,985
  • 1
  • 31
  • 39
Ospho
  • 2,756
  • 5
  • 26
  • 39
  • Is a duplicate of http://stackoverflow.com/questions/1174093/create-objective-c-class-instance-by-name – Woody Feb 23 '11 at 12:04

2 Answers2

1

I doubt that this is possible. But I'm not an expert for the inner core of objective-c.

I would suggest you to store your Projectile in a NSMutableDictionary. You could store the object with a key of @"homing_fireball". And then you can reference it with something like

Projectile *someProjectile = [myProjectiles objectForKey:tempenemy.atktype];
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

If i am getting what you mean, you are trying to init a projectiles object with a atktype as its member? You call in main..

Projectiles* tempProjectiles = [[Projectiles alloc]initWithType:@"homing_fireball"];

Your projectiles.h

// blah blah blah
{
NSString* atktype;
}
@property (nonatomic, retain)NSString* atktype;
 -(id)initWithType:(NSString*)type;

Your projectiles.m

@synthesize atktype;
-(id)initWithType:(NSString*)type
{
      self = [super init];
         if(self)
          {
             atktype = type;
          }
     return self;
}
xuanweng
  • 1,939
  • 1
  • 11
  • 10