比较目标C中的NSNumber [英] Comparing NSNumbers in Objective C

查看:86
本文介绍了比较目标C中的NSNumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C的初学者,在这种情况下我有些困惑.我有以下代码:

I am a beginner at Objective-C and I am a bit confused at this scenario. I have the following code:

if (number1 < number2) {
    NSLog(@"THE FOLLOWING NUMBER ");
    NSLog(@"%@", number1);
    NSLog(@"IS LESS THAN");
    NSLog(@"%@", number2);
}

当我运行这段代码时,我看到这样的结果真的很奇怪:

When I run this code I see really strange results like this:

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对象,怎么会这样呢?我通过查找屏幕上精灵之间的距离来获得两个数字.

Both numbers are NSNumber objects, how could something like this happen? I am getting the two numbers by finding distances between sprites on the screen.

任何线索或建议都将不胜感激

Any clues or advice would really be appreciated

推荐答案

我假设number1和number2是指向对象的指针. <符号正在比较指针.

I assume number1 and number2 are pointers to objects. The < sign is comparing the pointers.

您需要比较实际的floatValue或doubleValue

You need to compare the actual floatValue or doubleValue

if ([number1 doubleValue] < [number2 doubleValue]) 

....

这篇关于比较目标C中的NSNumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆