5

How to:

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

This doesn't seem to build

The object:

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
TheLearner
  • 19,387
  • 35
  • 95
  • 163
  • 1
    Title doesn't seem right. A NSInteger test would compile, I think you mean NSNumber. – jv42 Jan 25 '11 at 11:32

2 Answers2

16

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) {
}

Community
  • 1
  • 1
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
3

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)
{
 ...
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313