ObjectiveC Parse Integer from String

132,844

Solution 1

I really don't know what was so hard about this question, but I managed to do it this way:

[myStringContainingInt intValue];

It should be noted that you can also do:

myStringContainingInt.intValue;

Solution 2

You can just convert the string like that [str intValue] or [str integerValue]

integerValue Returns the NSInteger value of the receiver’s text.

  • (NSInteger)integerValue Return Value The NSInteger value of the receiver’s text, assuming a decimal representation and skipping whitespace at the beginning of the string. Returns 0 if the receiver doesn’t begin with a valid decimal text representation of a number.

for more information refer here

Solution 3

NSArray *_returnedArguments = [serverOutput componentsSeparatedByString:@":"];

_returnedArguments is an array of NSStrings which the UITextField text property is expecting. No need to convert.

Syntax error:

[_appDelegate loggedIn:usernameField.text:passwordField.text:(int)[[_returnedArguments objectAtIndex:2] intValue]];

If your _appDelegate has a passwordField property, then you can set the text using the following

[[_appDelegate passwordField] setText:[_returnedArguments objectAtIndex:2]];
Share:
132,844
Christian Stewart
Author by

Christian Stewart

Updated on July 08, 2022

Comments

  • Christian Stewart
    Christian Stewart almost 2 years

    I'm trying to extract a string (which contains an integer) from an array and then use it as an int in a function. I'm trying to convert it to a int using intValue.

    Here's the code I've been trying.

      NSArray *_returnedArguments = [serverOutput componentsSeparatedByString:@":"];
      [_appDelegate loggedIn:usernameField.text:passwordField.text:(int)[[_returnedArguments objectAtIndex:2] intValue]];
    

    I get this error:

    passing argument 3 of 'loggedIn:::' makes pointer from integer without a cast

    What's wrong?

  • Chuck
    Chuck over 13 years
    It's not necessarily supposed to be an object. He could have mistakenly declared the method as taking an int* — in fact, that seems pretty likely to me, since a lot of people who write Objective-C without learning the basics of C assume that parameter types just include stars as part of their syntax.
  • Chuck
    Chuck over 13 years
    I think you have been radically thrown off by the weird method name and awkwardly written invocation. The property is not "expecting a string" (whatever that means) — passwordField.text is the second argument to the loggedIn::: method and [[_returnedArguments objectAtIndex:2] intValue] is the third argument. Nothing is being passed to a property.
  • falconcreek
    falconcreek over 13 years
    i declare the method name an error.. inferring what the code is attempting to do...
  • Chuck
    Chuck over 13 years
    NSString does have an intValue method. developer.apple.com/iphone/library/documentation/cocoa/…
  • Jorge Israel Peña
    Jorge Israel Peña over 13 years
    Yeah, it's incorrect. What you want is just int (or better yet, NSInteger, which is the same thing). You only need * signs before objective-c objects, and an int is not an object, it's a primitive data type. So change the method signature to just (int) or (NSInteger), and it should fix it.
  • ToddBFisher
    ToddBFisher over 12 years
    Just for us noobs out there coming from other languages, [myStringContainingInt intValue]; can also be written like myStringContainingInt.intValue;
  • Christian Stewart
    Christian Stewart about 11 years
    You answer this after tons of answers for the same thing are already here?
  • volting
    volting almost 11 years
    What happens when the number inside myStringContainingInt is too big to fit inside 32 bits--actually 31 bits since it's signed?
  • phreakhead
    phreakhead over 10 years
    Are you kidding me? What if the string is "0"? Is it an error or a correct parse? Kind of sad that Apple abhors exceptions, since this is exactly the place you need them.
  • dulgan
    dulgan about 10 years
    You should use longValue in this case
  • Leo Flaherty
    Leo Flaherty over 8 years
    longLongValue to be precise
  • zekel
    zekel over 8 years
    It's important to note that you -[NSString intValue] may not behave exactly how you expect. From the docs: This property is 0 if the string doesn’t begin with a valid decimal text representation of a number. For example, [@" 2 asdghjk" intValue] will return 2.