Re: Why does NSArray count return NSUInteger?
Re: Why does NSArray count return NSUInteger?
- Subject: Re: Why does NSArray count return NSUInteger?
- From: Kyle Sluder <email@hidden>
- Date: Sun, 29 May 2011 13:14:11 -0700
On Sun, May 29, 2011 at 1:04 PM, julius <email@hidden> wrote:
> Hi,
> I have just spent time investigating why
> an if statement involving an [array count] was apparently misbehaving.
>
> The construct was this:
> if(3 < ([zAry count] - 10))
> It delivers a (to me unexpected) result when [zAry count] < 10.
>
> In fact
> if(3 >= ([zAry count] - 10))
> also returns an unexpected result for the same [zAry count] value.
>
> The reason is that [zAry count] returns a result of type NSUInteger!!!!
>
> Thus for this type of comparison I need to coerce the type to NSInteger i.e.
> if(3 < ((NSInteger)[zAry count] - 10))
>
>
> Why might the Cocoa developers have chosen to do this?
Because it doesn't make sense for an array to have fewer than 0 elements?
Silent signed/unsigned promotion issues are known behavior in the C
language. It is your responsibility to understand the code you're
writing and anticipate them accordingly.
--Kyle Sluder
_______________________________________________
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