Objective-C:如何检查一个数字是否在两个值之间 [英] Objective-C: How to check a number is between two values

查看:50
本文介绍了Objective-C:如何检查一个数字是否在两个值之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里有点疯狂,有一个超级菜鸟问题.如何检查和 int 是否在两个值之间,即想查看 x<100 &&x>50 我试过了:

Going a little nuts here with a super noob issue. How do a check if and int is between two values i.e. want to see if x<100 && x>50 I have tried:

if(x<100 && x>50){
 ..
}

但没有任何快乐??

推荐答案

除非您的编译器损坏,否则该代码没有任何问题.x

Unless your compiler is broken there is nothing wrong with that code. There is probably something wrong with x

正如 Sascha 在评论中所说,确保 x 不是 NSNumber.这样做的原因是如果 x 是一个 NSNumber 那么存储在其中的值是一个指针,该值可能远高于 100(例如 0x4FBC60),你会想要与 -(int)intValue 进行比较.

As Sascha stated in the comments make sure that x is not an NSNumber. The reason for this is if x is an NSNumber then the value stored in it is a pointer which value would likely be much higher than 100 (0x4FBC60 for example) and you would want to compare against the -(int)intValue.

其他需要考虑的事情是与正确的数据进行比较.虽然隐式数字转换效果很好,但您可能希望使用与数据类型相同的文字比较.

Other things to consider are comparing against the right data. While implicit number conversions work well you may want to use the same literal comparison as your data type.

unsigned long long x = 51ull;
if(x > 50ull && x < 100ull)
{

}

这篇关于Objective-C:如何检查一个数字是否在两个值之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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