- Making a copy of an Array
- 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];
- }
- referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems];
- for ( int i = 1; i <= numStems; i++ ) {
- // Fill in referenceArray
- }
- stemArray = [referenceArray copy];
- // 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 = [referenceArray mutableCopy]; // If stemArray is defined as an NSMutableArray
- stemArray = [referenceArray copy]; // If stemArray is defined as an NSArray