84

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";
john
  • 1,189
  • 2
  • 15
  • 20
  • 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)` – Alex Jul 09 '09 at 16:17
  • 1
    If the answers bellow confuse you, check out this related question: http://stackoverflow.com/questions/3414644/how-to-convert-integer-to-string-in-objective-c-closed – Florin Dec 15 '10 at 13:18

7 Answers7

185

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]
Scott
  • 16,711
  • 14
  • 75
  • 120
182

You can use literals, it's more compact.

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

(Boxes as a NSNumber and uses its stringValue method)

Robert
  • 37,670
  • 37
  • 171
  • 213
  • 4
    @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. – Robert Mar 24 '14 at 09:52
  • I prefer this over stringWithFormat, short and concise. – Satheesh Mar 09 '21 at 07:27
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"])
grahamparks
  • 16,130
  • 5
  • 49
  • 43
12

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

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Terry Wilcox
  • 9,010
  • 1
  • 34
  • 36
9
int val1 = [textBox1.text integerValue];
int val2 = [textBox2.text integerValue];

int resultValue = val1 * val2;

textBox3.text = [NSString stringWithFormat: @"%d", resultValue];
Robert
  • 37,670
  • 37
  • 171
  • 213
Amol
  • 341
  • 1
  • 3
  • 12
5

Simply convert int to NSString use :

  int x=10;

  NSString *strX=[NSString stringWithFormat:@"%d",x];
maazza
  • 7,016
  • 15
  • 63
  • 96
Ashwini Chougale
  • 1,093
  • 10
  • 26
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);
leonardosccd
  • 1,503
  • 11
  • 12