2

I'm just learning. In this tutorial, they've declared a block that takes NSUInteger arguments, and then they simply pass in 6 and 4 . . . I was under the impression all these NS things are object wrappers, different from the built-in c-types.

Now that I'm writing this, I remember them saying earlier that we didn't need the * in NSUInteger declarations because it wasn't like the other NS objects.

But...an NSUInteger is obviously not a c-type. So...what's going on here?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

3 Answers3

2

Short Answer: Sometimes, NSUInteger is "unsigned int". But sometimes it is not. It depends the platform you compile your code.

Long Answer:

NSUInteger or NSInteger is defined in the NSObjCRuntime.h, you can Command+click on an NSUInteger and see the define below:

#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

So, generally speaking, it means in a 64-bit environment, NSUInteger is an alias of unsigned long, while in a 32 bit environment, it is an int.

Because the new iPhone 5s is running on a 64-bit CPU, so it is recommended to use NSUInteger or NSInteger instead the plain unsign int or int.

And at last, NSUInteger is a real c-type indeed, in another name so the complier can do some work for you to handle it better.

onevcat
  • 4,591
  • 1
  • 25
  • 31
1

NSUInteger is like unsigned integer NSInteger is like int

See

When to use NSInteger vs. int

Community
  • 1
  • 1
user4951
  • 32,206
  • 53
  • 172
  • 282
1

If, in XCode, you option-Click on NSInteger you'll go to the definition:

#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

So for 64-bit systems, it is defined as long, not as int.

Merlevede
  • 8,140
  • 1
  • 24
  • 39