Skip to content

Instantly share code, notes, and snippets.

@capnslipp
Created May 13, 2013 05:52
Show Gist options
  • Save capnslipp/5566398 to your computer and use it in GitHub Desktop.
Save capnslipp/5566398 to your computer and use it in GitHub Desktop.
Quick test to find out what happens if an NSMutableArray is changed significantly within a for…in loop. (Modified from the ViewController.m from Xcode's iOS Single View Application project template.)
//
// ViewController.m
// LoopErrorTest
//
// Created by Slipp D. on 5/13/13.
// Copyright (c) 2013 me!. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray* array = [NSMutableArray new];
[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
int loopCount = 0;
for (NSString *element in array) {
NSLog(@"element %d: %@", loopCount, element);
if (loopCount == 2) {
[array removeLastObject];
[array removeLastObject];
[array removeLastObject];
}
++loopCount;
}
[array release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment