简体   繁体   English

将自定义类对象添加到NSArray会更新现有对象

[英]Adding custom class object to an NSArray updates existing objects

I've got a UIView in a viewController: 我在viewController中有一个UIView:

myView = [[rhythmUIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:myView];

The UIView exposes an NSArray in its .h: UIView在其.h中公开一个NSArray:

@interface rhythmUIView : UIView
@property () NSMutableArray* myHits;
@end

And instantiates and initializes it in the UIView's .m: 并在UIView的.m中实例化和初始化它:

NSMutableArray* myHits;
myHits = [[NSMutableArray alloc] init];

To which I add objects of a custom class: 我向其添加自定义类的对象:

@interface hits : NSObject
@property () double hitTime;
@property () float xPosition;
@end

in the viewController.m using 在viewController.m中使用

hits *thisHit;
thisHit = [[hits alloc] init];
<set thisHit's properties>
[myView.myHits addObject:thisHit];

All of that works - no compile or runtime errors, but when I change the values of the thisHit object in preparation to add a new object to the myHit array, it updates the value of every object that was previously inserted using thisHit. 所有这些都有效-没有编译或运行时错误,但是当我更改thisHit对象的值以准备向myHit数组中添加新对象时,它将更新以前使用thisHit插入的每个对象的值。

This seems like an attribute problem, so I added a property to the custom class: 这似乎是一个属性问题,因此我向自定义类添加了一个属性:

@property (copy) NSNumber* test;

And set it with: 并设置为:

thisHit.test = [NSNumber numberWithFloat:arc4random()%100];

Before the addobject. 在添加对象之前。 But it also modifies every row when I touch thisHit. 但是当我触摸thisHit时,它也会修改每一行。

I didn't expect adding "copy" to the array to work: 我没想到在数组中添加“ copy”即可工作:

@property (copy) NSMutableArray* myHits;

And it didn't. 事实并非如此。 Same results. 结果相同。

I even tried adding this to the ViewControoler.m: 我什至尝试将其添加到ViewControoler.m中:

@property (copy) hits* thisHit;

Same results. 结果相同。

Tried insertObject:atIndex: instead of addObject: Same results. 尝试使用insertObject:atIndex:而不是addObject:结果相同。

I messed around with strong and weak in desperation, but then it actually started crashing. 我在绝望中强弱相处,但实际上它开始崩溃了。

Finally, learning from my last post, I tried moving the instantiation of the array from the UIView.m to the UIView creation in the viewController.m: 最后,从我的上一篇文章中学习,我尝试将数组的实例从UIView.m移到viewController.m中的UIView创建中:

myView.myHits = [NSMutableArray new];

I had high hopes for that one, but again, no compile or runtime errors, but it was worse. 我对此抱有很高的期望,但同样,没有编译或运行时错误,但情况更糟。 The addobject doesn't actually do anything - the nsarray.count remains at zero. addobject实际上不执行任何操作-nsarray.count保持为零。

Help please? 请帮助? Thanks! 谢谢!

but when I change the values of the thisHit object in preparation to add a new object to the myHit array, it updates the value of every object that was previously inserted 但是,当我更改thisHit对象的值以准备向myHit数组添加新对象时,它会更新以前插入的每个对象的值

This is the problem. 这就是问题。 You should be creating a new hits object for each member of the array. 您应该为数组的每个成员创建一个新的 hits对象。 When you add an object to an array, the array simply adds a pointer to that object. 将对象添加到数组时,该数组仅将指针添加到该对象。 Hence, you are repeatedly adding the same object to the array. 因此,您要重复将同一对象添加到数组。 So every time you change that object, every object in the array appears to change. 因此,每次更改该对象时,数组中的每个对象似乎都会更改。

If you only call thisHit = [[hits alloc] init]; 如果只调用thisHit = [[hits alloc] init]; once then there is only one hits instance, and you are adding that single instance to the array multiple times. 一次,只有一个hits实例,并且您将单个实例多次添加到阵列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM