Convert NSInteger to NSUInteger?

SimplyKiwi picture SimplyKiwi · Aug 7, 2011 · Viewed 25.4k times · Source

I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this?

Answer

aroth picture aroth · Aug 7, 2011

NSInteger and NSUInteger are just typedefs for primitive integer types:

#if __LP64__ || NS_BUILD_32_LIKE_64
  typedef long NSInteger;
  typedef unsigned long NSUInteger;
#else
  typedef int NSInteger;
  typedef unsigned int NSUInteger;
#endif

As such, you don't need to "convert" between them. A simple cast should be sufficient. Like:

NSInteger myInt = 0;
NSUInteger unsignedInt = (NSUInteger)myInt;