90

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.

Espresso
  • 4,722
  • 1
  • 24
  • 33
Yo_Its_Az
  • 2,043
  • 2
  • 18
  • 25

11 Answers11

265

See the NSString Class Reference.

NSString *string = @"5";
int value = [string intValue];
Espresso
  • 4,722
  • 1
  • 24
  • 33
22

How about

[@"7" intValue];

Additionally if you want an NSNumber you could do

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter numberFromString:@"7"];
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • 6
    good lord NSNumberFormatter is complex ... i mean "feature complete". – Dave Dopson Apr 08 '12 at 00:37
  • 4
    Watch out - in iOS7 `numberFromString` seems to be an _instance_ method rather than a _class_ one, so you should do `[[NSNumberFormatter new] numberFromString:@"7"];` instead. – mgarciaisaia Aug 06 '14 at 17:11
  • @mgarciaisaia shouldn't we do `[[[NSNumberFormatter alloc] init] numberFromString:@"7"];` as per http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new – Michael Osofsky Aug 30 '14 at 20:09
3

I use:

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

And vice versa:

NSString* intToString(NSInteger integer) {
    return [NSString stringWithFormat:@"%d", integer];
}
max_
  • 24,076
  • 39
  • 122
  • 211
  • 1
    check your return value. NSString* – AWF4vk Jul 09 '11 at 23:21
  • I am using data pulled from NSUserDefaults so what should I put after the NSString *string = – Yo_Its_Az Jul 09 '11 at 23:26
  • Will either of these actually work? Surely the definitions need a semi colon or you will crash with `unrecognized selector` because you will actually be sending `@selector(intToString:)` not `@selector(intToString)`. Surely it should be like this instead `- (NSInteger) stringToInt:(NSString *)string;` and `- (NSString *)intToString:(NSInteger)integer;` – Paul.s Jul 09 '11 at 23:27
  • 1
    They're both C functions. It needs to be `NSString *` though, like @David said. – Espresso Jul 09 '11 at 23:30
  • @Espresso cheers I have no background in C so makes more sense now – Paul.s Jul 09 '11 at 23:31
  • I hand wrote it rather than running it through Xcode, thats why the * was missing. – max_ Jul 09 '11 at 23:33
  • +1 for being the only answer that correctly uses integerValue instead of intValue which is better able to handle the difference between 32-bit and 64-bit architectures. – Abizern Feb 08 '13 at 13:41
2

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

iAnkit
  • 1,940
  • 19
  • 25
2

Swift 3.0:

Int("5")

or

let stringToConvert = "5"
Int(stringToConvert)
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
1

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;
natur3
  • 236
  • 1
  • 3
  • 14
0

Yet another way: if you are working with a C string, e.g. const char *, C native atoi() is more convenient.

dotslashlu
  • 3,361
  • 4
  • 29
  • 56
0

You can also use like :

NSInteger getVal = [self.string integerValue];

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
newbee
  • 17
  • 6
0

To convert an String number to an Int, you should do this:

let stringNumber = "5"
let number = Int(stringNumber)
Ale Mohamad
  • 372
  • 4
  • 7
0
NSString *string = /* Assume this exists. */;
int value = [string intValue];
Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
0

Very easy..

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

Jacob
  • 1,459
  • 2
  • 19
  • 32