Objective-C: How To Covert A String Into An Integer

One the common things you need to do in any programming language is convert a string (e.g. “12”) into a corresponding integer. Coming from Ruby, I was pretty much taking this task for granted – there is a very simple “to_i” method (e.g. “12”.to_i) would do it.

Looking through Apple’s API Reference for NSString, I found that the same function is not too bad in Objective-C, although the syntax is much longer than in Ruby as usual. Just use the intValue method like this:

[@"12" intValue];

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.