ios 如何比较两个NSInteger?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9476743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two NSInteger?
提问by Ahsan
How do we compare two NSInteger numbers ? I have two NSIntegers and comparing them the regular way wasnt working.
我们如何比较两个 NSInteger 数字?我有两个 NSIntegers 并以常规方式比较它们不起作用。
if (NSIntegerNumber1 >= NSIntegerNumber2) {
//do something
}
Eventhough, the first value was 13 and the second value was 17, the if loop is executing
尽管第一个值是 13,第二个值是 17,但 if 循环正在执行
Any idea ?
任何的想法 ?
回答by justin
NSIntegeris just a typedef for a builtin integral type (e.g. intor long).
NSInteger只是内置整数类型(例如intor long)的 typedef 。
It is safe to compare using a == b.
使用 进行比较是安全的a == b。
Other common operators behave predictably: !=, <=, <, >=et al.
其他常见运算符的行为可预测:!=、<=、<、>=等。
Finally, NSInteger's underlying type varies by platform/architecture. It is not safe to assume it will always be 32 or 64 bit.
最后,NSInteger的底层类型因平台/架构而异。假设它总是 32 位或 64 位是不安全的。
回答by lnafziger
Well, since you have Integer and Number in the name, you might have declared the two values as NSNumber instead of NSInteger. If so, then you need to do the following:
好吧,由于名称中包含 Integer 和 Number,您可能已将这两个值声明为 NSNumber 而不是 NSInteger。如果是这样,那么您需要执行以下操作:
if ([NSIntegerNumber1 intValue] >= [NSIntegerNumber2 intValue]) {
// do something
}
Otherwise it should work as is!
否则它应该按原样工作!
回答by Rama Rao
NSInteger int1;
NSInteger int2;
int1 = 13;
int2 = 17;
if (int1 > int2)
{
NSLog(@"works");
}
回答by wizH
When comparing integers, using this, would work just fine:
比较整数时,使用这个,会工作得很好:
int a = 5;
int b = 7;
if (a < b) {
NSLog(@"%d is smaller than %d" a, b);
}

