我们如何比较两个NSInteger数字?我有两个NSIntegers并比较它们常规方式不起作用 .
if (NSIntegerNumber1 >= NSIntegerNumber2) { //do something }
尽管如此,第一个值是13,第二个值是17,if循环正在执行
任何的想法 ?
NSInteger int1; NSInteger int2; int1 = 13; int2 = 17; if (int1 > int2) { NSLog(@"works"); }
好吧,既然你的名字中有Integer和Number,你可能已经将这两个值声明为NSNumber而不是NSInteger . 如果是这样,那么您需要执行以下操作:
if ([NSIntegerNumber1 intValue] >= [NSIntegerNumber2 intValue]) { // do something }
否则它应该按原样工作!
NSInteger 只是内置整数类型的typedef(例如 int 或 long ) .
NSInteger
int
long
使用 a == b 进行比较是安全的 .
a == b
其他常见操作符可预测: != , <= , < , >= 等 .
!=
<=
<
>=
最后, NSInteger 的基础类型因平台/架构而异 . 假设它总是32位或64位是不安全的 .
比较整数时,使用它,可以正常工作:
int a = 5; int b = 7; if (a < b) { NSLog(@"%d is smaller than %d" a, b); }
4 回答
好吧,既然你的名字中有Integer和Number,你可能已经将这两个值声明为NSNumber而不是NSInteger . 如果是这样,那么您需要执行以下操作:
否则它应该按原样工作!
NSInteger
只是内置整数类型的typedef(例如int
或long
) .使用
a == b
进行比较是安全的 .其他常见操作符可预测:
!=
,<=
,<
,>=
等 .最后,
NSInteger
的基础类型因平台/架构而异 . 假设它总是32位或64位是不安全的 .比较整数时,使用它,可以正常工作: