Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.98 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Making a copy of an Array
  2. referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems];
  3.     referenceArray = [[NSMutableArray alloc] init];
  4.  
  5.     stemArray =  [NSMutableArray arrayWithCapacity:numberOfStems];
  6.     stemArray =  [[NSMutableArray alloc] init];
  7.  
  8. for ( int i = 1; i <= numStems; i++ ) {
  9.     NSString *soundName = [NSString stringWithFormat:@"stem-%i", i];
  10.     NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
  11.     NSURL *soundFile = [[NSURL alloc] initFileURLWithPath:soundPath];  
  12.     [referenceArray addObject:soundFile];
  13. }
  14.        
  15. referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems];
  16.  
  17. for ( int i = 1; i <= numStems; i++ ) {
  18.     // Fill in referenceArray
  19. }
  20.  
  21. stemArray = [referenceArray copy];
  22.        
  23. // You are making a new mutable array that has a starting capacity of numberOfStems and assigning it to the referenceArray variable
  24. referenceArray = [NSMutableArray arrayWithCapacity:numberOfStems];
  25.  
  26. // 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
  27. referenceArray = [[NSMutableArray alloc] init];
  28.  
  29. // Same as above
  30. stemArray =  [NSMutableArray arrayWithCapacity:numberOfStems];
  31. stemArray =  [[NSMutableArray alloc] init];
  32.  
  33. for ( int i = 1; i <= numStems; i++ ) {
  34.     // This part looks fine
  35.     NSString *soundName = [NSString stringWithFormat:@"stem-%i", i];
  36.     NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
  37.     NSURL *soundFile = [[NSURL alloc] initFileURLWithPath:soundPath];  
  38.     [referenceArray addObject:soundFile];
  39.  
  40.     // If you are in ARC, you are fine.  If non-ARC, you are leaking soundFile and need to do:
  41.     // [soundFile release];
  42. }
  43.        
  44. stemArray = [referenceArray mutableCopy];  // If stemArray is defined as an NSMutableArray
  45.        
  46. stemArray = [referenceArray copy];  // If stemArray is defined as an NSArray