Adding or Removeing Items Using Fast Enumeration

On page 126 in the paragraph just before the title: “Old-style array methods”. It states:[quote]For Future reference, when using fast enumeration with an NSMutableArray, you are not allowed to add or remove items while iterating over an array. If you need to add or remove items while iterating, you must use a standard for-loop.[/quote]Well, I keyed in the fast enumeration for loop and I noticed that even though I was adding and removing items, the program still worked. I played around with it before coming on to this forum but it seems to work with the for loop and with the fast enumeration for loop. I’ll put the code below so you can see what I am doing.[code]@autoreleasepool
{
/* create 4 NSDate objects */
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
NSDate *dayBeforeYesterday = [now dateByAddingTimeInterval:-48.0 * 60.0 * 60.0];

    /* create an array containing all 3 */
    /* NSArray *dateList = @[now, tomorrow, yesterday]; */
  
    /* an empty mutable array can be created 2 ways, 1st way using alloc and init. The 2nd way is used. */
    /* NSMutableArray *dateList = [[NSMutableArray alloc] init]; */
    NSMutableArray *dateList = [NSMutableArray array];
    
  
    /* add 2 dates to the array using the addObject: method. 
       This method adds the objects at the end of the list. */
    [dateList addObject:now];
    [dateList addObject];
  
    /* add yesterday at a specific index (in this case at the beginning) using insertObject:at Index*/
    [dateList insertObject: yesterday atIndex:0];
    /* add another day at index:0 & yesterday is moved up to atIndex:1 to make room for the new addition */
    [dateList insertObject: dayBeforeYesterday atIndex:0];
  
    [dateList removeObjectAtIndex:2];

    /* print a couple of dates */
    NSLog(@"today's date is %@.", dateList[2]);
    NSLog(@"yesterday's date is %@.", dateList[1]);
  
    /* prints out how many dates are in the array */
    NSLog(@"There are %lu dates", [dateList count]); /* count states the number of objects in the array */
  
    /* iterate of the array */
/* NSUInteger dateCount = [dateList count]; // the arrays's item count is used to limit the number of
 /* for (int i =0; i < dateCount; i++)      // times the loop will run to prevent an out-of-range error
      {
        NSDate *d = dateList[i];
      } */
  
    for (NSDate *d in dateList)            /* done using fast inumeration replaceing the above for loop */
      {                                    /* fast inumeration is very efficient way to go through the  */
        NSLog(@"Here is a date: %@", d);   /* items in an array */
      }
  }

[/code] I don’t get it.

[color=#000040]JR[/color]

Maybe I misread your code, but I don’t think you are mutating your array in your fast loop. You are only displaying the dates.

Yeah, ballgeier is right, he is not adding anything in the array. I have tried it and it’s only possible to mutate the array when using ‘for’ statement to iterate. If you try to mutate in “fast enumeration” the program crash.