私は2つのNSMutableArray
を持っています。どちらも同じデータを取得しています。私の問題は、別の辞書の両方の可変配列に同じデータを割り当て、1つの操作を実行すると、両方の配列に影響しているということです。NSMutableArrayが別のNSMutableArrayに影響を与えていますか?
replaceObjectAtIndex:WithObject:
のような操作を初めて実行すると、配列は影響を受けませんが、2番目の置換が呼び出されると、両方の配列が置き換えられた値を持ちます。私はそれが参考の問題だと思う。
誰にも解決策がありますか?
NSMutableArrays
の名前はhelper.urlsRecording
とhelper.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).
他の配列への参照のコピーを防止するにはどうすればよいですか?
ボタンをクリックして:
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);
}
注:NSMutableArray
アクセスにクラスでグローバルに定義されています。
にオブジェクトを追加辞書&
copy
/mutableCopy
を取りますか? –@ReinierMelian編集を確認してください – Williams
はい、私はデータを取得している理由を初期化しています。 – Williams