Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert NSInteger to NSString datatype?

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]];
like image 524
senthilM Avatar asked Nov 25 '09 11:11

senthilM


3 Answers

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

like image 68
luvieere Avatar answered Oct 29 '22 05:10

luvieere


Obj-C way =):

NSString *inStr = [@(month) stringValue];
like image 32
Alexey Kozhevnikov Avatar answered Oct 29 '22 06:10

Alexey Kozhevnikov


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)
like image 23
MadNik Avatar answered Oct 29 '22 06:10

MadNik