Maybe array indexes and counts are unsigned because you can't have a negative 
number of objects in an array?

_murat

On May 29, 2011, at 1:04 PM, julius 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?
> Julius
> 
> 
> ======
> Here to satisfy possible curiosity is my test code snippet and results
>       NSMutableArray * zAry = [[NSMutableArray alloc]init];
>       for(NSInteger i = 0; i < 7; i++) {
>               [zAry addObject:@"obj"];
>       }
>       
>       NSLog(@"[zAry count] = %d",[zAry count]);
>       
>       if(3 < ([zAry count] - 9)) {
>               NSLog(@"A1: wrong result for: 3 < %d",([zAry count] - 9));
>       } else {
>               NSLog(@"A2: correct result for: 3 < %d",([zAry count] - 9));
>       }
>       
>       if(3 >= ([zAry count] - 9)) {
>               NSLog(@"B1: correct result for: 3 >= %d",([zAry count] - 9));
>       } else {
>               NSLog(@"B2: wrong result for: 3 >= %d",([zAry count] - 9));
>       }
>       
>       NSInteger zAryCount = [zAry count];
>       if(3 < (zAryCount - 10)) {
>               NSLog(@"C1: wrong result for: 3 < %d",(zAryCount - 9));
>       } else {
>               NSLog(@"C2: correct result for: 3 < %d",(zAryCount - 9));
>       }
>       
>       NSUInteger zUInt = 7;
>       if (3 < (zUInt - 9)) {
>               NSLog(@"E1: wrong result for: 3 < %d",(zUInt - 9));
>       } else {
>               NSLog(@"E2: correct result for: 3 < %d",(zUInt - 9));
>       }
>       
>       // these produce the required result
>       if(3 < ((NSInteger)[zAry count] - 9)) {
>               NSLog(@"F1: wrong result for: 3 < %d",([zAry count] - 9));
>       } else {
>               NSLog(@"F2: correct result for: 3 < %d",([zAry count] - 9));
>       }
>       
>       if(3 >= ((NSInteger)[zAry count] - 9)) {
>               NSLog(@"G1: correct result for: 3 >= %d",([zAry count] - 9));
>       } else {
>               NSLog(@"G2: wrong result for: 3 >= %d",([zAry count] - 9));
>       }
> 
> 2011-05-29 20:41:19.107 TestIf[4858:903] [zAry count] = 7
> 2011-05-29 20:41:19.110 TestIf[4858:903] A1: wrong result for: 3 < -2
> 2011-05-29 20:41:19.110 TestIf[4858:903] B2: wrong result for: 3 >= -2
> 2011-05-29 20:41:19.111 TestIf[4858:903] C2: correct result for: 3 < -2
> 2011-05-29 20:41:19.111 TestIf[4858:903] E1: wrong result for: 3 < -2
> 2011-05-29 20:41:19.111 TestIf[4858:903] F2: correct result for: 3 < -2
> 2011-05-29 20:41:19.112 TestIf[4858:903] G1: correct result for: 3 >= -2
> 
> 
> http://juliuspaintings.co.uk
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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/mlist0987%40gmail.com
> 
> This email sent to mlist0...@gmail.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to