无效的接收器类型int

时间:2010-07-02 09:40:44

标签: iphone xcode warnings int

我似乎无法找到这里会出现什么问题......

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);

事情是:它起作用,'从'和'到'得到正确的值,但我在指示的行上得到警告......

任何人都知道为什么以及如何解决这个问题? (编译时不要喜欢它们警告;))

1 个答案:

答案 0 :(得分:3)

temp已经是int值,没有NSNumber。因此,您无法向其发送[temp intValue]消息。

只需使用

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

编辑:以下是证明其有效的代码:

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);

输出符合预期4,99。