创建一个对象,奇怪的问题

发布于 2024-10-19 05:15:16 字数 327 浏览 4 评论 0原文

我有一个类“Projectiles”,我想用它创建一个对象。 为了最小化代码,我想从字符串中指定对象,这会清理很多东西。

例子: 我有字符串,

tempenemy.atktype = @"homing_fireball";

现在我想从 Projectiles 类创建一个同名的对象:

Projectiles *tempenemy.atktype;

这可能吗?那么最终结果将是来自 Projectiles 类的一个名为 homing_fireball 的对象..?

谢谢!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-10-26 05:15:16

我怀疑这是可能的。但我不是objective-c内核的专家。

我建议您将 Projectile 存储在 NSMutableDictionary 中。您可以使用@“homing_fireball”键存储该对象。然后你可以用类似的东西来引用它

Projectile *someProjectile = [myProjectiles objectForKey:tempenemy.atktype];

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];
彼岸花似海 2024-10-26 05:15:16

如果我明白你的意思,你正在尝试初始化一个以 atktype 作为其成员的射弹对象?你在 main 中调用..

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

你的projectiles.h

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

你的projectiles.m

@synthesize atktype;
-(id)initWithType:(NSString*)type
{
      self = [super init];
         if(self)
          {
             atktype = type;
          }
     return self;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文