views:

1107

answers:

4

Anyone have the expertise to explain when to use NSUInteger and when to use NSInteger?

I had seen Cocoa methods returning NSInteger even in cases where the returned value will always be unsigned.

What is the fundamental reason? Is NSInteger or int strictly limited to if we want to represent negative value?

From NSObjCRuntime.h:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
+3  A: 

If your method has a suitably restricted output range, you might as well use NSInteger since it's easier to type. As you say, if you need to return negative numbers, NSInteger is the only game in town; I'd only use NSUInteger if I needed to return really big numbers for some reason.

Carl Norum
For even bigger numbers, use NSDecimalNumber for 38 places of base-10 precision.
codewarrior
+1, @codewarrior, only if you can pass classes around, though.
Carl Norum
I go the other way: I use NSUInteger at all times, except when I need negative numbers. Also, you mean objects, not classes; there are very few reasons to pass classes around, and even fewer reasons to pass the NSDecimalNumber class specifically.
Peter Hosey
I guess it really depends on our needs.
Jesse Armand
A: 

I don't know about cocoa specifically, but usually the only disadvantage of signed integers is they usually have half the maximum value as a unsigned int. Such as about 2 billion instead of 4 billion for 32bit systems. Generally this is not a big difference, since if you are dealing with values close to 2 billion, you probably would be just as worried about a 4 billion max in the same case, since it is still putting pretty close to an overflow with just a multiplication of 2 or 3.

Signed integers are usually proferred because the additional flexibility and fact the a signed integer can be used in almost all the scenarios an unsigned integer can plus all the additoinal scenarios it cannot where a negative is needed.

Unsigned might be preferred if you want to enforce positive only values.

AaronLS
I added the definitions of NSInteger and NSUInteger.
Jesse Armand
signed integers are sometimes used even when the normal range is positive if you want to be able to initialize a variable to an "invalid" value such as -1.
progrmr
A: 

Out of interest, can you give an example of a method that returns an NSInteger that can never be negative (including any documented special-case values)?

Ahruman
Take a look at UITableViewDataSource protocols, all of them return NSInteger. While NSIndexPath is using NSUInteger.
Jesse Armand
So they do. That looks unmotivated and therefore broken. :-/
Ahruman
+1  A: 

You should also be aware of integer conversion rules when dealing with NSUInteger vs. NSInteger:

The following fragment for example returns 0 (false) although you'd expect it to print 1 (true):

NSInteger si = -1;
NSUInteger ui = 1;
printf("%d\n", si < ui);

The reason is that the [si] variable is being implicitly converted to an unsigned int!

See CERT's Secure Coding site for an in-depth discussion around these 'issues' and how to solve them.

Erik Abele