Why use [string1 intValue] in lieu of atoi

Discussion in 'Mac Programming' started by poppy11, Nov 1, 2013.

  1. poppy11, Nov 1, 2013
    Last edited: Nov 1, 2013

    poppy11 macrumors newbie

    Oct 30, 2013
    I am very new to Objective C, and am using the Smyth book Objective-C 2.0 Essentials-Third Edition. I'm working through the string stuff.

    So why would I do something like ii=[string1 intValue];

    when in the native 'C', I can just do ii=atoi(string1);

    I'm not being sarcastic; I assume that the intValue method expands functionality in some way, or they wouldn't have bothered.

    Sorry, trying to find the delete button. I believe that I've figured it out on my own.

    In the book, he used an example like @"3456", which will work in either case, but assuming that string1 is an object, then atoi() wouldn't work.
  2. subsonix macrumors 68040

    Feb 2, 2008
    String1 is not a C string. We can know this because it responds to method calls, it's a string object that returns the int value of the string as a response to the intValue call. The internal representation can be any of the available encodings.

    atoi() only return the int value of primitive C strings, that is a byte string terminated by a null character.
  3. poppy11 thread starter macrumors newbie

    Oct 30, 2013

Share This Page