On  Sun, 29 May 2011 18:15:10 -0400
> 
> From: Jeffrey Walton <noloa...@gmail.com> wrote
> As Kyle said, its the C language - signed values are
> promoted/converted (?) to unsigned. So -1 is always greater than 1 (if
> you let it happen). Its really the generated CMP instruction which is
> bitting you. Cast the unsigned to an NSInteger (be mindful of overflow
> first).
> 
> The compiler and/or clang should have warned you about it. A clean
> compile using -Wall -Wextra is not difficult with Cocoa/Cocoa Touch. I
> add the switches under "Other C Flags" (its easier than checking
> boxes). Also turn on clang. If you find you have a lot of interfaces
> and those interfaces have a lot of unused parameters, add
> -Wno-unused-parameter.

In replying to this email I take the opportunity also to reply to Siegfried, 
Murat and Kyle.

I thank you all for your replies and advice especially regarding the use of 
clang.
I would also like immediately to clarify that I have no problem accepting the 
existence and usage of different numeric types. Moreover the moment I saw that 
NSArray’s count method returned an NSUInteger I knew why my code wasn’t working 
but that wasn’t my question. Let me repeat it.

Why did Cocoa developers make the count method return NSUInteger?

Both Murat and Kyle said this was because it does not make sense for an array 
to have a negative number of elements. True but why because of that fact make 
the output an NSUInteger?

In what contexts is that output going to be used?
I think that like me people will tend to use it in contexts to do with 
accessing array elements
e.g. x = [ary objectAtIndex:[ary count]-2];
or modifying behaviour according to array size 
e.g. if(k1 < ([ary count] - k2)) {…

I don’t often use the "if [array count]-x" formulation which is why I got into 
trouble and why I’m asking the question. 

If we know that a variable is never going to return a negative value and that 
it is predominantly going to be used in an arithmetic context why force the 
programmer to think about coercing the type on each occasion of its use when 
failure to do so risks malfunction?

Moreover, as Jeffrey said, the cause of this programming error is subtle:
“Its really the generated CMP instruction which is bitting you.”.
Exactly. 
NSInteger x = [ary count] - 3; delivers the unproblematic result 
but if(x < ([ary count] - 3)) does not.

So was it really just because the number of array elements is never -3 that the 
Cocoa developers decided to make NSArray count return type NSUInteger? 

Julius

 


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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to