2012-01-25 13 views
1

私は2つの可変配列を作成しました - referencesArray & stemArray、次に私はURLでreferenceArrayを埋めます。 stemArrayにreferenceArrayの正確なコピーを作成したいと思います。私はそれを作ります 割り当てstemArray = referenceArray;正しくない(私はこれを試してみると奇妙なことが起きる)。 2番目のループを作成するより良い方法があるはずです&そのようにstemArrayを埋める?私はまだポインタではあまり快適ではない&私はこの状況が潜在的な地雷であると信じています...ヒントや提案はありますか?事前のおかげで:)配列のコピーを作成する

referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems]; 
    referenceArray = [[NSMutableArray alloc] init]; 

    stemArray = [NSMutableArray arrayWithCapacity:numberOfStems]; 
    stemArray = [[NSMutableArray alloc] init]; 

for (int i = 1; i <= numStems; i++) { 
    NSString *soundName = [NSString stringWithFormat:@"stem-%i", i]; 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"]; 
    NSURL *soundFile = [[NSURL alloc] initFileURLWithPath:soundPath]; 
    [referenceArray addObject:soundFile]; 
} 

答えて

3

あなたがそれらを作成した直後に、あなたの可変配列へのポインタを上書きしている - なぜそこにあるものをalloc/init行がありますか?あなたがreferenceArrayを移入した後

referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems]; 

for (int i = 1; i <= numStems; i++) { 
    // Fill in referenceArray 
} 

stemArray = [referenceArray copy]; 
+0

(これは、それがこのケースで働いていた技術的に間違っていた本家の)私のプログラムはのalloc/initをラインなしで正しく実行されないだろう - 私は何を必要とすることは ReferenceArrましたay = [[NSMutableArray alloc] initWithCapacity:numberOfStems]; 助けてくれてありがとう、 'コピー'メッセージは私が探していたものでした。 – Octave1

2

なぜあなたはちょうど&のinit stemArrayをALLOCことができませんでした:あなたがNSArrayののコピーをしたい場合は、単にそれにcopyメッセージを送りますか?

はこのような何かを:

stemArray = [[NSMutableArray alloc] initWithArray: referenceArray];

また、DOUBLEのallocのあなたがそこにやっている(すなわちarrayWithCapacityライン)を取り除きます。

1

ここにいくつかの問題があります。のは、既存のコードのステップバイステップを見てみましょう:あなたのオリジナルの説明に基づいて

// You are making a new mutable array that has a starting capacity of numberOfStems and assigning it to the referenceArray variable 
referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems]; 

// You then create another new mutable array with the default capacity and re-assign the referenceArray variable. Fortunately, the first array was created with -arrayWithCapacity: instead of -init...; thus, you aren't leaking an object 
referenceArray = [[NSMutableArray alloc] init]; 

// Same as above 
stemArray = [NSMutableArray arrayWithCapacity:numberOfStems]; 
stemArray = [[NSMutableArray alloc] init]; 

for (int i = 1; i <= numStems; i++) { 
    // This part looks fine 
    NSString *soundName = [NSString stringWithFormat:@"stem-%i", i]; 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"]; 
    NSURL *soundFile = [[NSURL alloc] initFileURLWithPath:soundPath]; 
    [referenceArray addObject:soundFile]; 

    // If you are in ARC, you are fine. If non-ARC, you are leaking soundFile and need to do: 
    // [soundFile release]; 
} 

、あなたはおそらく最後までstemArray宣言を移動し、-copyまたは-mutableCopyを使用したい:

stemArray = [referenceArray mutableCopy]; // If stemArray is defined as an NSMutableArray 

か:

stemArray = [referenceArray copy]; // If stemArray is defined as an NSArray 
関連する問題