views:

266

answers:

5

Hello!

Really basic iPhone Objective-C question. I'm trying to extract a string (which is really a int) 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 the error passing argument 3 of 'loggedIn:::' makes pointer from integer without a cast

Thanks for the help,

Christian Stewart

A: 

I guess you do not need the (int) part

rano
It's true that you don't need it, but that wouldn't lead to the error he's seeing.
Chuck
A: 

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
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
Yeah, hadn't thought of that. That's a good possibility.
Jorge Israel Peña
It takes int *. s this incorrect?
Christian Stewart
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
A: 
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
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
i declare the method name an error.. inferring what the code is attempting to do...
falconcreek
Yeah you are very wrong at this lol. That's not what I meant. Chuck was correct. Thanks for the answer however.
Christian Stewart
A: 

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
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
Pardon, you are right.
beefon
+1  A: 

I really don't know what was so hard about this question, but I managed to do it this way [myStringContainingInt intValue];

That easy.

Thanks for your effort though.

Christian Stewart