Skip to content

November 2, 2014

Convert from string to integer in Objective C

by noise

Next example shows you how to convert a string to integer value.

NSString *aString = @"200";
 
int anInt = (int)[aString integerValue];
NSLog(@"anInt = %d", anInt);

Note that we use cast to int (int) otherwise we get a warning: “Implicit conversion loses integer precision: ‘NSInteger’ (aka ‘long’) to ‘int’“. Instead of cast to int we could define anInt as long.

Read more from Objective C Tips

Leave a Reply

required
required

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments