views:

20

answers:

1

Hi!

I cant sem to find what would be the problem here...

NSArray *oneMove;
oneMove = [[bestMoves objectAtIndex:i] componentsSeparatedByString:@","];
int from, to;
int temp = [[oneMove objectAtIndex:0] intValue];

from = [temp intValue]/100; //"Invalid receiver type int"
to = [temp intValue]%100;   //"Invalid receiver type int"

NSLog(@"%d, %d", from, to);

The thing is: it works and 'from' and 'to' get the right values but i get warnings at the indicated lines...

anyone knows why and how to fix that? (dont like them warnings when compiling ;) )

thx

+2  A: 

temp is already int value, no NSNumber. So you cannot send an [temp intValue] message to it.

Just use

from = temp / 100;
to = temp % 100;

Edit: Here is code that proves it works:

NSArray *bestMoves = [NSArray arrayWithObject:@"499,340,124"]; // Example data
NSArray *oneMove  = [[bestMoves objectAtIndex:0] componentsSeparatedByString:@","];
int from, to;
int temp = [[oneMove objectAtIndex:0] intValue];

from = temp/100; // Code change
to = temp%100;   // Code change

NSLog(@"%d, %d", from, to);

Output is as expected 4, 99.

Eiko
tried that but that doesn give me the right outcome. temp HAS to have only 4 digits like 3132 (which is what i get if i do it my way) but if i do what you suggested i get 97759792 instead...
zwadl
I did not make any change to temp. Have you checked that oneMove does hold the value you expect? I guess it should be a NSNumber object. Then do NSLog("%d",temp); to see if this works. If it doesn't, your data model is broken.
Eiko
the oneMove array contains 2 NSCFString objects... (as does the bestMoves Array) might that help in your analysis?
zwadl
btw: thisint temp = [[[oneMove objectAtIndex:0] intValue] intValue];gives me the same correct result... but still there is the invalid receiver thingy...
zwadl
thx! your answer is certanly correct, but my problem started else where ;) the sting that gets seperated was composed in this way [NSString stringWithFormat:@"%d,%d", [possibleMoves objectAtIndex:i], wert]; when actually it should be like that: [NSString stringWithFormat:@"%d,%d", [[possibleMoves objectAtIndex:i] intValue], wert];thanks anyway! you helped me find the right tracks!
zwadl
Did you actually do the code change? You cannot send intValue to an int. Please see my updated code.
Eiko
yes! after i changed the way how i composed the string your code change worked fine. what im guessing is that at first i saved a pointer to the string instead of an int. so where i got the invalid receiver type warning its true that it would have sent the intValue message to an int but this int was at the same time a pointer and the compiler found the correct value for this... or something like that... anyway the problem originated a lot earlier in the code...
zwadl

related questions