Creating an object, strange question
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 ob开发者_如何学编程ject 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!!
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];
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;
}
精彩评论