I have a ad layout program which builds pages (NSView class Page) and on each page places margins and gutters (more NSViews) and then ads (NSView class Ad). The app auto places ads from a database source, looking for the proper space to fill on each page. The user then has the option to move the ads from say page 4 to page 2. They also have the option to manually flow the ads or have the app autoflow, the assumption being that the ad you just moved will end up in the 0/0 x/y location and then the others will flow to any available space, being pushed to the next page if there is no space on the current one (due to the ad you just moved).
What I was doing was getting all the pages (themselves subviews of an NSView called masterpage) and then looping through them to get all of their subviews into an array. Loop through that array, filtering for margins and gutters and if it is an ad remove it from the superview. My thought was to start with a clean page and replace all the ads. Problem is I keep getting this error:
Collection <NSCFArray: 0x1078900> was mutated while being enumerated.
I'm using this code:
//loop through all the pages
for(NSView* thisPage in masterPageSubViews) {
//get all the ads on this page
NSArray* thisPageSubviews = [thisPage subviews];
for(NSView* thisPageSubview in thisPageSubviews) {
if ([[thisPageSubview className] isEqual:@"Ad"]) {
[thisPageSubview removeFromSuperview];
}
}
}
I guess I am confused as to why I am getting this error. I thought the objects in the array were separate from the objects being created in the loop.
Since the number of ads on each page is dynamic how can I strip them from their superview without utilizing an array?