On 29/12/2009, at 1:03 PM, Henry McGilton (Boulevardier) wrote:

>> Wouldn't this fall foul of the usual problem with deleting items from the 
>> collection being iterated, in that it will skip every other object? (Or 
>> perhaps fast enumeration has a built-in protection against that?)
> 
> Yes, fast enumeration specifically states:
> 
>       • Enumeration is “safe”—the enumerator has a mutation guard so that if 
> you attempt to modify the collection during enumeration, an exception is 
> raised.
> And also:
> 
>         Since mutation of the object during iteration is forbidden, you can 
> perform multiple enumerations concurrently.
> 

So, if -trackingAreas returns the actual array (rather than a copy), and 
-removeTrackingArea: is basically a wrapper for -removeObject: on that same 
array then the loop will fail with this exception. Since we don't know if 
either of these things are true, then the loop as written will either fail, or 
depend upon implementation details which could change for its success.

However that's a different problem from the one I mentioned, where a forward 
iteration of an array that removes the indexed item will end up skipping every 
other object. Iterating a copy of the array will avoid that, so I was wondering 
if fast enumeration operated on a copy internally. I'm thinking 'no', since a) 
it wouldn't be so fast, b) there would be no need for the exception you mention 
and c) there are plenty of cases where a copy is undesirable or unnecessary.

So however you look at it, the suggested replacement for the original faulty 
loop is just as faulty.

I'd just go with:

while([[self trackingAreas] count] > 0 )
    [self removeTrackingArea:[[self trackingAreas] lastObject]];


--Graham


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to