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 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • Tanner Middleton

    I find Objective-C’s syntax very interesting. Being a .NET developer myself it looks slightly foreign compared to:

    (int32) “12”;
    or
    Convert.ToInt32(“12”);