简体   繁体   English

NSMutableArray是否正在影响另一个NSMutableArray?

[英]NSMutableArray is affecting the another NSMutableArray?

I have two NSMutableArray s, both getting the same data. 我有两个NSMutableArray ,都获取相同的数据。 My problem is that when I assign the same data to both the mutable arrays from different dictionaries and perform operations on one NSMutableArray , it is affecting both arrays. 我的问题是,当我将来自不同字典的可变数据分配给两个可变数组并在一个NSMutableArray上执行操作时,这会影响两个数组。

When I perform operations like replaceObjectAtIndex:WithObject: , the first time, the array is not affected but when the second replace is called both arrays have the replaced value. 第一次执行诸如replaceObjectAtIndex:WithObject:类的操作时,该数组不受影响,但是在调用第二个replace时,两个数组都具有替换值。 I think it is a reference issue. 我认为这是一个参考问题。

Does anyone have a solution to this? 有人对此有解决方案吗?

Name of the NSMutableArrays is helper.urlsRecording and helper.holdingArr . NSMutableArrays名称是helper.urlsRecordinghelper.holdingArr

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
[dict setValue:outputFileURL forKey:@"URL"];
[dict setValue:@"1" forKey:@"index"];
[dict2 setValue:outputFileURL forKey:@"URL"];
[dict2 setValue:@"1" forKey:@"index"];
[helper.urlsRecording addObject:dict];
[helper.holdingArr addObject:dict2];


[helper.urlsRecording replaceObjectAtIndex:button.tag withObject:urlAr];//When this called second time, both the arrays is effected(helper.urlsRecording as well as helper.holdingArr).

How can I prevent the copying of the reference to another array? 如何防止将引用复制到另一个数组?

Button Click: 点击按钮:

if([button isSelected] == NO){

     NSLog(@"Url Recording : %@",helper.urlsRecording);
     [[helper.urlsRecording objectAtIndex:button.tag] removeObjectForKey:@"URL"];
     button.selected = YES;
     NSLog(@"Url Recording : %@",helper.urlsRecording);
}
else{
     [helper.urlsRecording replaceObjectAtIndex:button.tag withObject:[helper.holdingArr objectAtIndex:button.tag]];
     button.selected = NO;
     NSLog(@"Url Recording : %@",helper.urlsRecording);

 }

Note: NSMutableArray is defined globally in a class to access. 注意: NSMutableArray是在要访问的类中全局定义的。

This is because your instance values are same for both dictionary. 这是因为两个字典的实例值相同。

So First create one mutableDictionary like below 所以首先创建一个mutableDictionary如下

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:outputFileURL forKey:@"URL"];
[dict setValue:@"1" forKey:@"index"];

And create second dictionary through mutableCopy, so instance will be different for both. 并通过mutableCopy创建第二个字典,因此两者的实例将有所不同。

NSMutableDictionary *dict2 = [dict mutableCopy];

After that you can add them in to NSMutableArray and update accordingly. 之后,您可以将它们添加到NSMutableArray并进行相应的更新。

Take a copy / mutableCopy dictionary & then add object to the MutableArray copy / mutableCopy字典,然后将对象添加到MutableArray

[helper.urlsRecording addObject:[dict copy]];
[helper.holdingArr addObject:[dict2 copy]];

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

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