Convert string to int

I am having trouble converting a string to an integer. I am googled, but all I can find is how to convert int to string. Does anyone know how to do this the other way around? Thank.

+87
ios objective-c xcode ios4
Jul 09 '11 at 23:11
source share
11 answers

See the reference to the NSString class .

NSString *string = @"5"; int value = [string intValue]; 
+263
Jul 09 '11 at 23:20
source share

What about

 [@"7" intValue]; 

Alternatively, if you want NSNumber , you can do

 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter numberFromString:@"7"]; 
+22
Jul 09 '11 at 23:12
source share

I use:

 NSInteger stringToInt(NSString *string) { return [string integerValue]; } 

And vice versa:

 NSString* intToString(NSInteger integer) { return [NSString stringWithFormat:@"%d", integer]; } 
+3
Jul 09 '11 at 23:18
source share

Swift 3.0:

 Int("5") 

or

 let stringToConvert = "5" Int(stringToConvert) 
+2
Jan 03 '17 at 23:43 on
source share

This is a simple solution to convert a string to int

 NSString *strNum = @"10"; int num = [strNum intValue]; 

but when you get the value from the text box,

 int num = [txtField.text intValue]; 

where txtField is the output of UITextField

+1
Nov 19 '14 at 6:45
source share

I should have done something similar, but wanted to use a getter / setter for mine. In particular, I wanted to return a long one from a text box. Other answers to all worked well too, I just ended up adapting my project a bit as my school project developed.

 long ms = [self.textfield.text longLongValue]; return ms; 
+1
Jul 30 '15 at 20:21
source share
 NSString *string = /* Assume this exists. */; int value = [string intValue]; 
0
Jul 09 '11 at 23:14
source share

Very simple..

int (name of integer) = [(name of string, no ()) intValue];

0
Jul 11 2018-11-11T00:
source share

Another way: if you are working with string C, for example. const char *, C native atoi() more convenient.

0
Jul 21 '13 at 9:07 on
source share

You can also use like:

NSInteger getVal = [self.string integerValue];

0
Apr 3 '14 at 13:00
source share

To convert a String number to Int , you must do this:

 let stringNumber = "5" let number = Int(stringNumber) 
0
May 20 '19 at 3:37
source share



All Articles