2017-08-31 28 views
-1

私は2つのNSMutableArrayを持っています。どちらも同じデータを取得しています。私の問題は、別の辞書の両方の可変配列に同じデータを割り当て、1つの操作を実行すると、両方の配列に影響しているということです。NSMutableArrayが別のNSMutableArrayに影響を与えていますか?

replaceObjectAtIndex:WithObject:のような操作を初めて実行すると、配列は影響を受けませんが、2番目の置換が呼び出されると、両方の配列が置き換えられた値を持ちます。私はそれが参考の問題だと思う。

誰にも解決策がありますか?

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). 

他の配列への参照のコピーを防止するにはどうすればよいですか?

ボタンをクリックして:

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アクセスにクラスでグローバルに定義されています。

+0

にオブジェクトを追加辞書& copy/mutableCopyを取りますか? –

+0

@ReinierMelian編集を確認してください – Williams

+0

はい、私はデータを取得している理由を初期化しています。 – Williams

答えて

3

これは、両方の辞書のインスタンス値が同じであるためです。

だから、まず

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

以下のようなものmutableDictionaryを作成し、mutableCopyを介して第2の辞書を作成し、そのインスタンスは、両方のために異なるだろう。

NSMutableDictionary *dict2 = [dict mutableCopy]; 

これをNSMutableArrayに追加して、それに応じて更新することができます。

+0

問題は同じですが、2回目にデータが配列に参照されていると、ボタンが2回目にクリックされたときの参照は同じです。 – Williams

+0

ボタンクリックで何をしていますか?コードを表示する –

+0

編集を確認してください – Williams

0

は、どのようにあなたは、この二つの配列を初期化しているMutableArray

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

私はこれを試してみます。 – Williams

+0

okey。これはあなたのために働いたのですか? –

+0

仲間はいません。働いていない。 – Williams