• 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: Should I do loops in reverse order ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Should I do loops in reverse order ?


  • Subject: Re: Should I do loops in reverse order ?
  • From: Jeff C <email@hidden>
  • Date: Mon, 27 Nov 2006 21:40:13 -0500


On Nov 27, 2006, at 12:45 PM, Sean McBride wrote:

On 2006-11-27 09:22, Matt Neuburg said:

What I would do is:

int i,u=[myArray count];
for (i=0; i<u; i++)

Be careful using this pattern if myArray is mutable and the contents are changing during iteration.


The value of u must be updated or you may run past the end of the array and get an exception.

I generally use the form


for ( i=0; i < [theArray count]; i++ )


when i'm not using NSEnumerator and hope that the implementer was smart enough to make the -count method as fast as possible.


Granted, that's probably not a good assumption if you're trying to squeeze every last cycle out of the machine, but for most of the applications I write, the sacrifice of performance is worth it to avoid bugs.




Matt,

I know you've just typed this in mail, but remember that -count returns
'unsigned int' not 'int'. :)


I'm not aware of any gcc warning that will catch that mistake (someone
prove me wrong, please!!). CodeWarrior will catch it though, even with
it's out of date Obj-C support: Warning : implicit arithmetic conversion
from 'unsigned int' to 'int'.


The OP made the same error:

int i;
for ( i=0 ; i<[myArray count] ; i++ ) {
	// do stuff
}

This gcc will catch if you turn on GCC_WARN_SIGN_COMPARE aka -Wsign- compare.

--
____________________________________________________________
Sean McBride, B. Eng                 email@hidden
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
40earthlink.net


This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: Should I do loops in reverse order ?
      • From: John Stiles <email@hidden>
References: 
 >Re: Should I do loops in reverse order ? (From: Matt Neuburg <email@hidden>)
 >Re: Should I do loops in reverse order ? (From: "Sean McBride" <email@hidden>)

  • Prev by Date: Invalid number in bound text field
  • Next by Date: Re: Should I do loops in reverse order ?
  • Previous by thread: Re: Should I do loops in reverse order ?
  • Next by thread: Re: Should I do loops in reverse order ?
  • Index(es):
    • Date
    • Thread