[英]How to compare a NSNumber in an if
How to: 如何:
if (myNSNumber == 1)
{
...
}
This doesn't seem to build 这似乎没有建立
The object: 物体:
If myNSNUmber is NSNumber
, your code should read, 如果myNSNUmber是
NSNumber
,则您的代码应为:
if ([myNSNumber intValue] == 1) {
...
}
If it is NSInteger
, you can directly compare it with an integer literal. 如果它是
NSInteger
,则可以直接将它与整数文字进行比较。 Because NSInteger
is a primitive data type. 因为
NSInteger
是原始数据类型。
if (myNSNumber == 1) {
...
}
Note: Make sure you don't have *
in your declaration. 注意:请确保声明中没有
*
。 Your NSInteger
declarations should read, 您的
NSInteger
声明应显示为:
NSInteger myNSNUmber; // RIGHT
NSInteger *myNSNUmber; // WRONG, NSInteger is not a struct, but it is a primitive data type.
The following is based on @BoltClock's answer, which he recently posted here 以下内容基于@BoltClock的答案,他最近在此处发布了答案
However if you do need to use a pointer to an NSInteger (that is, NSInteger *) for some reason, then you need to dereference the pointer to get the value: 但是,如果由于某种原因确实需要使用指向NSInteger的指针(即NSInteger *),则需要取消引用该指针以获取值:
if (*myNSNUmber == 11) {
}
NSInteger is normally a plain int type so your code should work fine. NSInteger通常是普通的int类型,因此您的代码应该可以正常工作。
if myNSNumber a NSNumber object (as variable name suggests) then you should extract its int value: 如果myNSNumber是一个NSNumber对象(如变量名所示),则应提取其int值:
if ([myNSNumber intValue] == 1)
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.