How do I convert NSInteger to NSString datatype?
Asked Answered
S

9

150

How does one convert NSInteger to the NSString datatype?

I tried the following, where month is an NSInteger:

NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
Spiccato answered 25/11, 2009 at 11:36 Comment(0)
S
281

NSIntegers are not objects, you cast them to long, in order to match the current 64-bit architectures' definition:

NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];

Sick answered 25/11, 2009 at 11:37 Comment(5)
I tried this, but I kept getting a warning Format specifies type 'int' but the argument has type 'NSInteger *'(aka 'int *'). Instead according to Apple docs, I went with NSString *inStr = [NSString stringWithFormat:@"%d", (int)month];Antebellum
Note that on 64-bit processors, such as the new A7 chip, if your app is compiled for 64-bit, an NSInteger is actually a long, not an int. Doing the cast, (int)month would be destructive on 64-bit platforms for the generic case. If targeting Apple platforms exclusively, prefer the Objective-C way as in Aleksey Kozhevnikov's answer, or something similar that will work with both int and long -- e.g. long ;-) An example, though unsigned (meaning non-negative) is in Andreas Ley's answer.Magalimagallanes
@Antebellum I've tried to delete the answer in order to have the current, more applicable ones, surface and be accepted, but apparently, accepted answers cannot be deleted. I've therefore tried to at least adjust its contents in order to provide as much useful information for people looking for a quick solution that doesn't trigger warnings.Sick
@Sick Apple's documentation clearly explains how to treat NSInteger in format strings. You should update your answer to follow this advise.Austerlitz
[@(integerValue) stringValue] is a cleaner approach.Drowsy
L
193

Obj-C way =):

NSString *inStr = [@(month) stringValue];
Legion answered 21/4, 2013 at 13:33 Comment(0)
R
86

Modern Objective-C

An NSInteger has the method stringValue that can be used even with a literal

NSString *integerAsString1 = [@12 stringValue];

NSInteger number = 13;
NSString *integerAsString2 = [@(number) stringValue];

Very simple. Isn't it?

Swift

var integerAsString = String(integer)
Retainer answered 14/12, 2013 at 6:36 Comment(0)
R
8

%zd works for NSIntegers (%tu for NSUInteger) with no casts and no warnings on both 32-bit and 64-bit architectures. I have no idea why this is not the "recommended way".

NSString *string = [NSString stringWithFormat:@"%zd", month];

If you're interested in why this works see this question.

Representational answered 14/5, 2015 at 17:20 Comment(0)
E
5

Easy way to do:

NSInteger value = x;
NSString *string = [@(value) stringValue];

Here the @(value) converts the given NSInteger to an NSNumber object for which you can call the required function, stringValue.

Eskisehir answered 22/6, 2016 at 11:11 Comment(0)
S
2

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

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
Sim answered 6/12, 2013 at 22:25 Comment(0)
W
2

You can also try:

NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];
Washerwoman answered 6/2, 2014 at 9:18 Comment(0)
W
0

The answer is given but think that for some situation this will be also interesting way to get string from NSInteger

NSInteger value = 12;
NSString * string = [NSString stringWithFormat:@"%0.0f", (float)value];
Woody answered 21/4, 2013 at 13:13 Comment(0)
A
0

NSNumber may be good for you in this case.

NSString *inStr = [NSString stringWithFormat:@"%d", 
                    [NSNumber numberWithInteger:[month intValue]]];
Agan answered 12/8, 2014 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.