How do I convert NSInteger to NSString datatype?
Solution 1:
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];
Solution 2:
Obj-C way =):
NSString *inStr = [@(month) stringValue];
Solution 3:
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)
Solution 4:
%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.