• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: File Extensions Problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: File Extensions Problem


  • Subject: Re: File Extensions Problem
  • From: Graham Cox <email@hidden>
  • Date: Fri, 22 Aug 2008 20:23:54 +1000


On 22 Aug 2008, at 7:10 pm, Phil wrote:

From the -reverseObjectEnumerator 'special considerations' section of
the documentation:
"When you use this method with mutable subclasses of NSArray, you must
not modify the array during enumeration."


You shouldn't go modifying the array in general, for sure. But the special case of modification by deleting the last item in the array is safe, and always will be. In fact deleting any item with a higher index than current is safe, and always will be. Why? Because an ordinary for loop that counts down is free to do this and the existence of such loops in shipped code means that NSMutableArray can never be changed in such a way as to break code like that. (It's also hard to see how it could be, even theoretically).

-reverseObjectEnumerator is no different, because it "lets you access each object in the receiver, in order, from the element at the highest index down to the element at index 0". This statement will hold true for any future implementation of NSMutableArray, meaning that it is now, and always will be safe to delete items in the array at or higher than the current index. By the definition of an array, removing an item only affects the indexes of objects with equal or greater indexes than the removed item.

But be clear, this is a special case, so the warning about modification *in general* should be heeded.

In the OP's case of weeding out unwanted file extensions from a list, using a reverse enumerator to do this is quite safe. The argument may well be moot in these days of fast enumerators though. It's also trivial to make a copy and iterate that, as Andrew suggested, though with very huge arrays, that copy might be worth avoiding.

Graham
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: File Extensions Problem
      • From: "Michael Ash" <email@hidden>
    • Re: File Extensions Problem
      • From: Phil <email@hidden>
References: 
 >File Extensions Problem (From: Adil Saleem <email@hidden>)
 >Re: File Extensions Problem (From: Michael Dautermann <email@hidden>)
 >Re: File Extensions Problem (From: Andrew Merenbach <email@hidden>)
 >Re: File Extensions Problem (From: Graham Cox <email@hidden>)
 >Re: File Extensions Problem (From: Phil <email@hidden>)

  • Prev by Date: WebFrame stopLoading in delegate crashes app
  • Next by Date: Re: Displaying time
  • Previous by thread: Re: File Extensions Problem
  • Next by thread: Re: File Extensions Problem
  • Index(es):
    • Date
    • Thread