How do you convert an NSUInteger into an NSString?
Asked Answered
S

5

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);
Simulcast answered 1/3, 2011 at 14:11 Comment(0)
O
37

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

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
Orling answered 6/12, 2013 at 22:23 Comment(3)
or %zd for NSUInteger, without typecasting that is.Pneumatograph
@PaulPeelen Did you mean to say NSInteger?Orling
My bad.. I meant, of course, %tu (since its unsigned).Pneumatograph
M
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]];
Masturbation answered 3/3, 2011 at 9:20 Comment(3)
As of Xcode 4.5, this will throw a "Data argument not used by format string" warning. Does anyone know the correct format?Zajac
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.Zajac
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.Sulphone
P
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)

Pneumatograph answered 23/3, 2014 at 22:48 Comment(2)
Note that while tu works right now, it isn't guaranteed to. See here: #18894380Orling
Very true. I didn't know that actually. Good to keep in mind for the future.Pneumatograph
A
5

You can also use:

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

where row is a NSUInteger.

Averroes answered 11/12, 2015 at 7:14 Comment(0)
S
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.

Subdivide answered 1/11, 2016 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.