87

I am trying to convert from an int to a string but I am having trouble. I followed the execution through the debugger and the string 'myT' gets the value of 'sum' but the 'if' statement does not work correctly if the 'sum' is 10,11,12. Should I not be using a primitive int type to store the number? Also, both methods I tried (see commented-out code) fail to follow the true path of the 'if' statement. Thanks!

int x = [my1 intValue]; 
    int y = [my2 intValue]; 
    int sum = x+y;
    //myT = [NSString stringWithFormat:@"%d", sum];
    myT = [[NSNumber numberWithInt:sum] stringValue];

    if(myT==@"10" || myT==@"11" || myT==@"12")
        action = @"numGreaterThanNine";
2
  • 6
    Is there a reason you're putting the integer into a string? It would be so much easier to write your test as if (sum >= 10 && sum <= 12) Commented Jul 9, 2009 at 16:17
  • 1
    If the answers bellow confuse you, check out this related question: stackoverflow.com/questions/3414644/… Commented Dec 15, 2010 at 13:18

7 Answers 7

189

If you just need an int to a string as you suggest, I've found the easiest way is to do as below:

[NSString stringWithFormat:@"%d",numberYouAreTryingToConvert]
Sign up to request clarification or add additional context in comments.

Comments

185

You can use literals, it's more compact.

NSString* myString = [@(17) stringValue];

(Boxes as a NSNumber and uses its stringValue method)

2 Comments

@RavindranathAkila - stringWithFormat is more flexible / powerful and should be used in general. This is just a shortcut if its a simple conversion you are after.
I prefer this over stringWithFormat, short and concise.
33

The commented out version is the more correct way to do this.

If you use the == operator on strings, you're comparing the strings' addresses (where they're allocated in memory) rather than the values of the strings. This is very occasional useful (it indicates you have the exact same string object), but 99% of the time you want to compare the values, which you do like so:

if([myT isEqualToString:@"10"] || [myT isEqualToString:@"11"] || [myT isEqualToString:@"12"])

1 Comment

Slight typo, a missing : in the 2nd comparison.
12

== shouldn't be used to compare objects in your if. For NSString use isEqualToString: to compare them.

Comments

10
int val1 = [textBox1.text integerValue];
int val2 = [textBox2.text integerValue];

int resultValue = val1 * val2;

textBox3.text = [NSString stringWithFormat: @"%d", resultValue];

Comments

5

Simply convert int to NSString use :

  int x=10;

  NSString *strX=[NSString stringWithFormat:@"%d",x];

Comments

3

Dot grammar maybe more swift!

@(intValueDemo).stringValue 

for example

int intValueDemo  = 1;
//or
NSInteger intValueDemo = 1;

//So you can use dot grammar 
NSLog(@"%@",@(intValueDemo).stringValue);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.