• 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: NSUInteger in for-loop?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSUInteger in for-loop?


  • Subject: Re: NSUInteger in for-loop?
  • From: Jason Coco <email@hidden>
  • Date: Mon, 15 Sep 2008 00:51:28 -0400


On Sep 15, 2008, at 00:45 , Brett Powley wrote:


On 15/09/2008, at 2:15 PM, Alex Reynolds wrote:

I'm wondering if I'm using unsigned integers (specifically NSUInteger) properly or not.

I was under the impression that unsigned integers run from 0 to MAX_INT, but when I use them in a "for" loop within these bounds, the loop does not seem to always obey these constraints.

For example:

for (NSUInteger counter = 5; counter >= 0; --counter)
{
	NSLog(@"NSUInteger: %d", counter);
}

keeps running well after the "counter" variable turns negative:

First problem: %d is for signed integers; if you want to see the value, you want %u.
Second problem (which you'll see if you change the NSLog): counter is unsigned, so it *never* "turns negative".


The values of counter in your loop will be 5,4,3,2,1,0,MAX_UINT, MAX_UINT-1, etc

(Note that the range of values for unsigned integers is 0..MAX_UINT, not MAX_INT.)

Just to quickly reiterate, since NSUInteger changes size based on the architecture, it's important that you compare it to NSUIntegerMax...


Jason

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

References: 
 >NSUInteger in for-loop? (From: Alex Reynolds <email@hidden>)
 >Re: NSUInteger in for-loop? (From: Brett Powley <email@hidden>)

  • Prev by Date: Re: NSUInteger in for-loop?
  • Next by Date: Re: NSUInteger in for-loop?
  • Previous by thread: Re: NSUInteger in for-loop?
  • Next by thread: Re: NSUInteger in for-loop?
  • Index(es):
    • Date
    • Thread