Int to String in Objective-C

Find this useful? Support us: Star on GitHub 6
Category: Type | Language: Objective-C

Certainly! In Objective-C, you can convert an integer value to a string using the NSString class method stringWithFormat:. Here's an example:

``objective-c
int num = 42;
NSString *stringNum = [NSString stringWithFormat:@"%d", num];
NSLog(@"%@", stringNum); // Output: 42


In this example, we first declare the integer variable
num and set its value to 42. Then, we use the stringWithFormat: method to convert num to a string. The format specifier %d tells stringWithFormat: that we want to insert an integer value in the resulting string. Finally, we log the resulting string to the console using NSLog().

You can also use other format specifiers to format the integer value differently in the resulting string. Here's an example:

objective-c
int num = 42;
NSString *stringNum = [NSString stringWithFormat:@"The answer is %03d", num];
NSLog(@"%@", stringNum); // Output: The answer is 042
`

In this example, we use the format specifier %03d`, which pads the integer value with zeroes to a width of 3 characters. This results in the string "042" instead of "42". We also include the string "The answer is " before the formatted integer in the resulting string.

I hope this helps! Let me know if you have any further questions.