I'm having trouble converting a string into an integer. I googled it but all I can find is how to convert an int into a string. Does anyone know how to do it the other way around? Thanks.
This is the simple solution for converting string to int
NSString *strNum = @"10";
int num = [strNum intValue];
but when you are getting value from the textfield then,
int num = [txtField.text intValue];
where txtField is an outlet of UITextField
1
votes
I had to do something like this but wanted to use a getter/setter for mine. In particular I wanted to return a long from a textfield. The other answers all worked well also, I just ended up adapting mine a little as my school project evolved.
long ms = [self.textfield.text longLongValue];
return ms;
0
votes
NSString *string = /* Assume this exists. */;
int value = [string intValue];
0
votes
Very easy..
int (name of integer) = [(name of string, no ()) intValue];
0
votes
Yet another way: if you are working with a C string, e.g. const char *, C native atoi() is more convenient.
0
votes
You can also use like :
NSInteger getVal = [self.string integerValue];
0
votes
To convert an String number to an Int, you should do this:
let stringNumber = "5"
let number = Int(stringNumber)
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more