objective-cnsintegernsuinteger

Convert NSInteger to NSUInteger?


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


Solution

  • 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;