当类对象存储在数组中时,无法更改类对象属性,为什么不呢?
基本上,我创建一个数组并向其中添加自定义类的实例。但是,当我尝试访问存储在数组中的实例的属性时,我没有得到该类的响应。顺便说一句,它适用于类的常规实例。
//This works:
Laser *myLaser = [[Laser alloc] initWithPosX:100 Y:150 node:self];
myLaser.sprite.rotation = 50; //This changes the sprite¨s (the sprite inside the class) rotation
//This doesnt:
Laser *aLaser = [[Laser alloc] initWithPosX:100 Y:150 node:self];
[Lasers addObject:aLaser]; //Lasers is a NSMutableArray declared in the .h
[aLaser release];
Laser *copyLaser = [Lasers objectAtIndex:0];
copyLaser.sprite.rotation = 50; //For some reason this doesnt work!
有谁知道为什么它不起作用?使用 NSMutableArray 是否有问题?,我的类是 NSObject。
Basically I create an array and add instances of my custom class to it. But when i try to access properties of the instances stored in the array, I get no response from the class. Btw, it works with regular instances of a class.
//This works:
Laser *myLaser = [[Laser alloc] initWithPosX:100 Y:150 node:self];
myLaser.sprite.rotation = 50; //This changes the sprite¨s (the sprite inside the class) rotation
//This doesnt:
Laser *aLaser = [[Laser alloc] initWithPosX:100 Y:150 node:self];
[Lasers addObject:aLaser]; //Lasers is a NSMutableArray declared in the .h
[aLaser release];
Laser *copyLaser = [Lasers objectAtIndex:0];
copyLaser.sprite.rotation = 50; //For some reason this doesnt work!
Does anyone know why it doesnt work? Is it a problem with using NSMutableArray?, my class is an NSObject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里看到的问题是
Lasers
没有初始化并且在这里为零。您必须在某个地方初始化
Lasers
(可能是在使用它之前或在视图控制器的 init 方法中)。使用[[NSMutableArray alloc] init]
。The problem I see here is that
Lasers
is not initialized and is nil here.You have to initialize
Lasers
somewhere (probably just before you use it or in your init method of your view controller). Use[[NSMutableArray alloc] init]
.