ObjectiveC Parse Integer from String
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]];
Christian Stewart
Updated on July 08, 2022Comments
-
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 over 13 yearsIt'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 over 13 yearsI 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 theloggedIn:::
method and[[_returnedArguments objectAtIndex:2] intValue]
is the third argument. Nothing is being passed to a property. -
falconcreek over 13 yearsi declare the method name an error.. inferring what the code is attempting to do...
-
Chuck over 13 yearsNSString does have an
intValue
method. developer.apple.com/iphone/library/documentation/cocoa/… -
Jorge Israel Peña over 13 yearsYeah, 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 over 12 yearsJust for us noobs out there coming from other languages,
[myStringContainingInt intValue];
can also be written likemyStringContainingInt.intValue;
-
Christian Stewart about 11 yearsYou answer this after tons of answers for the same thing are already here?
-
volting almost 11 yearsWhat happens when the number inside
myStringContainingInt
is too big to fit inside 32 bits--actually 31 bits since it's signed? -
phreakhead over 10 yearsAre 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 about 10 yearsYou should use longValue in this case
-
Leo Flaherty over 8 yearslongLongValue to be precise
-
zekel over 8 yearsIt'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 return2
.