开发者

How to compare a NSNumber in an if

How to:

if (myNSNumber == 1开发者_如何学JAVA)
{
 ...
}

This doesn't seem to build

The object:


If myNSNUmber is NSNumber, your code should read,

if ([myNSNumber intValue] == 1) {
    ...
}

If it is NSInteger, you can directly compare it with an integer literal. Because NSInteger is a primitive data type.

if (myNSNumber == 1) {
    ...
}

Note: Make sure you don't have * in your declaration. Your NSInteger declarations should read,

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


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:

if (*myNSNUmber == 11) {
}


NSInteger is normally a plain int type so your code should work fine.

if myNSNumber a NSNumber object (as variable name suggests) then you should extract its int value:

if ([myNSNumber intValue] == 1)
{
 ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜