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]];
HangarRash
  • 7,314
  • 5
  • 5
  • 32

9 Answers9

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

luvieere
  • 37,065
  • 18
  • 127
  • 179
  • 11
    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](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html), I went with `NSString *inStr = [NSString stringWithFormat:@"%d", (int)month];` – Steven Apr 08 '13 at 19:48
  • 8
    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. – Louis St-Amour Feb 05 '14 at 05:11
  • 1
    @Steven 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. – luvieere Feb 18 '14 at 09:29
  • @luvieere [Apple's documentation](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html) clearly explains how to treat NSInteger in format strings. You should update your answer to follow this advise. – Nikolai Ruhe Oct 09 '14 at 07:08
  • 3
    `[@(integerValue) stringValue]` is a cleaner approach. – Zorayr Mar 25 '15 at 19:17
194

Obj-C way =):

NSString *inStr = [@(month) stringValue];
Alexey Kozhevnikov
  • 4,249
  • 1
  • 21
  • 29
85

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)
legends2k
  • 31,634
  • 25
  • 118
  • 222
MadNik
  • 7,713
  • 2
  • 37
  • 39
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.

Community
  • 1
  • 1
Kevin
  • 16,696
  • 7
  • 51
  • 68
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.

maxdev
  • 2,491
  • 1
  • 25
  • 50
Karthik damodara
  • 1,805
  • 19
  • 17
2

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
2

You can also try:

NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
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];
Nazir
  • 1,945
  • 24
  • 27
0

NSNumber may be good for you in this case.

NSString *inStr = [NSString stringWithFormat:@"%d", 
                    [NSNumber numberWithInteger:[month intValue]]];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
hothead
  • 131
  • 1
  • 3