26

How do you convert an NSUInteger into an NSString? I've tried but my NSString returned 0 all the time.

NSUInteger NamesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];  
NSLog(@"--- %d", NamesCategoriesNSArrayCount);  
[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%d",    NamesCategoriesNSArrayCount]];  
NSLog(@"=== %d", NamesCategoriesNSArrayCountString);
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
user563496
  • 271
  • 1
  • 3
  • 4

5 Answers5

37

When compiling with support for arm64, this won't generate a warning:

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
7

I hope your NamesCategoriesNSArrayCountString is NSString; if yes use the below line of code.

NamesCategoriesNSArrayCountString  = [NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]];

istead of

[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]];
DD_
  • 7,230
  • 11
  • 38
  • 59
Raj
  • 5,895
  • 4
  • 27
  • 48
  • As of Xcode 4.5, this will throw a "Data argument not used by format string" warning. Does anyone know the correct format? – GeneralMike Oct 01 '12 at 19:09
  • Disregard last. I had a typo that I didn't notice for the longest time, and the typo was what was causing the error. `[NSString stringWithFormat:@"%d", myNSUInteger]` is still the correct way to do this. – GeneralMike Oct 02 '12 at 16:48
  • 1
    The answer by @andreas-ley is more accurate. ``%d`` is the format string for a *signed* integer. NSUInteger is an *unsigned long* integer. Using ``%lu`` is needed once the overlap between the unsigned and signed range is exceeded, otherwise a negative value will be printed when in fact the number is positive. – Ben Jan 17 '14 at 22:18
6

When compiling for arm64, use the following to avoid warnings:

[NSString stringWithFormat:@"%tu", myNSUInteger];

Or, in your case:

NSUInteger namesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];  
NSLog(@"--- %tu", namesCategoriesNSArrayCount);  
[namesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%tu", namesCategoriesNSArrayCount]];  
NSLog(@"=== %@", namesCategoriesNSArrayCountString);

(Also, tip: Variables start with lowercase. Info: here)

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • 1
    Note that while `tu` works right now, it isn't guaranteed to. See here: http://stackoverflow.com/questions/18893880/alternatives-to-type-casting-when-formatting-nsuinteger-on-32-and-64-bit-archi – Andreas Ley Mar 24 '14 at 12:29
  • Very true. I didn't know that actually. Good to keep in mind for the future. – Paul Peelen Mar 24 '14 at 13:41
5

You can also use:

NSString *rowString = [NSString stringWithFormat: @"%@",  @(row)];

where row is a NSUInteger.

Reefwing
  • 2,242
  • 1
  • 22
  • 23
0

This String Format Specifiers article from Apple is specific when you need to format Apple types:

OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.

  • [NSString stringWithFormat:@"%ld", (long)value] : NSInteger displayed as decimal
  • [NSString stringWithFormat:@"%lx", (long)value] : NSInteger displayed as hex
  • [NSString stringWithFormat:@"%lu", (unsigned long)value] : NSUInteger displayed as decimal
  • [NSString stringWithFormat:@"%lx", (unsigned long)value] : NSUInteger displayed as hex
  • [NSString stringWithFormat:@"%f", value] : CGFloat
  • [NSString stringWithFormat:@"%ld", (long)value] : CFIndex displayed as decimal
  • [NSString stringWithFormat:@"%lx", (long)value] : CFIndex displayed as hex

See the article for more details.

Nate
  • 12,963
  • 4
  • 59
  • 80