123

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?

Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
  • 2
    How loggedIn::: is declared? especially what type of the 3rd parameter it expects? – Vladimir Aug 24 '10 at 17:21
  • 2
    Out of curiosity, what language are you coming from? I've seen a rash of people asking Objective-C questions on SO where they've made method calls that look like your `loggedIn:::` method, which is a decidedly un-Objective-C way to do things. – kubi Aug 24 '10 at 17:37
  • @kubi: it's Obj-C, method with arguments without names. – beefon Aug 24 '10 at 18:23
  • @beefon Obviously the code here is Objective-C. I'm wondering where @Christian learned to write code like that. – kubi Aug 24 '10 at 18:34
  • Well, the method looks like this lol: - (void)loggedIn:(NSString *)tusername:(NSString *)tpassword:(int)userId So its not really loggedIn:: lol. Any ideas? Username and password come through fine its just the int thing that was screwing with me... – Christian Stewart Aug 24 '10 at 21:28
  • 1
    No, it really is `loggedIn:::`. The rest of that declaration is just parameter names, not part of the selector. Idiomatically, it should be `- (void)loggedIn:(NSString *)tusername password:(NSString *)tpassword userID:(int)userId`. And then you would call it as `[_appDelegate userIsLoggedIn:usernameField.text password:passwordField.text userID:[[_returnedArguments objectAtIndex:2] intValue]];`. – Chuck Aug 24 '10 at 21:34
  • I guess you do not need the `(int)` part – rano Aug 24 '10 at 17:20
  • It's true that you don't need it, but that wouldn't lead to the error he's seeing. – Chuck Aug 24 '10 at 17:26

6 Answers6

271

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;

Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
  • 2
    Just for us noobs out there coming from other languages, `[myStringContainingInt intValue];` can also be written like `myStringContainingInt.intValue;` – ToddBFisher Jan 29 '12 at 06:14
  • 5
    What happens when the number inside `myStringContainingInt` is too big to fit inside 32 bits--actually 31 bits since it's signed? – Josh Hinman Jun 20 '13 at 19:50
  • You should use longValue in this case – Dulgan Apr 08 '14 at 07:41
  • 1
    longLongValue to be precise – Leo Flaherty Nov 05 '15 at 19:04
  • 7
    It's important to note that you `-[NSString intValue]` may not behave exactly how you expect. From the [docs](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/#//apple_ref/occ/instp/NSString/intValue): _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`. – zekel Dec 02 '15 at 15:37
8

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

Danyun Liu
  • 3,072
  • 24
  • 22
  • 3
    You answer this after tons of answers for the same thing are already here? – Christian Stewart Mar 07 '13 at 16:35
  • 7
    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. – phreakhead Oct 19 '13 at 08:06
1
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]];
falconcreek
  • 4,170
  • 1
  • 21
  • 22
  • 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. – Chuck Aug 24 '10 at 17:31
  • i declare the method name an error.. inferring what the code is attempting to do... – falconcreek Aug 24 '10 at 17:35
0

Basically, the third parameter in loggedIn should not be an integer, it should be an object of some kind, but we can't know for sure because you did not name the parameters in the method call. Provide the method signature so we can see for sure. Perhaps it takes an NSNumber or something.

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
  • 1
    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 Aug 24 '10 at 17:29
  • 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. – Jorge Israel Peña Aug 24 '10 at 23:38
0

Keep in mind that international users may be using a decimal separator other than . in which case values can get mixed up or just become nil when using intValue on a string.

For example, in the UK 1.23 is written 1,23, so the number 1.777 would be input by user as 1,777, which, as .intValue, will be 1777 not 1 (truncated).


I've made a macro that will convert input text to an NSNumber based on a locale argument which can be nil (if nil it uses device current locale).

#define stringToNumber(__string, __nullable_locale) (\
(^NSNumber *(void){\
NSLocale *__locale = __nullable_locale;\
if (!__locale) {\
__locale = [NSLocale currentLocale];\
}\
NSString *__string_copy = [__string stringByReplacingOccurrencesOfString:__locale.groupingSeparator withString:@""];\
__string_copy = [__string_copy stringByReplacingOccurrencesOfString:__locale.decimalSeparator withString:@"."];\
return @([__string_copy doubleValue]);\
})()\
)
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
-1

If I understood you correctly, you need to convert your NSString to int? Try this peace of code:

NSString *stringWithNumberInside = [_returnedArguments objectAtIndex:2];
int number;
sscanf([stringWithNumberInside UTF8String], "%x", &flags);
beefon
  • 3,194
  • 1
  • 21
  • 25
  • 4
    NSString does have an `intValue` method. http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue – Chuck Aug 24 '10 at 18:53