我是 Objective-C 的初学者,我对这种情况有点困惑。我有以下代码:
if (number1 < number2) {
NSLog(@"THE FOLLOWING NUMBER ");
NSLog(@"%@", number1);
NSLog(@"IS LESS THAN");
NSLog(@"%@", number2);
}
当我运行这段代码时,我看到了这样奇怪的结果:
2011-07-06 20:38:18.044 helloworld[1014:207] THE FOLLOWING NUMBER
2011-07-06 20:38:18.047 helloworld[1014:207] 190.8776
2011-07-06 20:38:18.050 helloworld[1014:207] IS LESS THAN
2011-07-06 20:38:18.053 helloworld[1014:207] 96.75866
这两个数字都是 NSNumber 对象,怎么会发生这样的事情呢?我通过查找屏幕上 Sprite 之间的距离来获得这两个数字。
任何线索或建议将不胜感激
最佳答案
我假设 number1 和 number2 是指向对象的指针。 < 符号正在比较指针。
你需要比较实际的floatValue或doubleValue
if ([number1 doubleValue] < [number2 doubleValue])
....
关于objective-c - 比较 Objective C 中的 NSNumbers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6605262/