9

I have an int value defined in one of my method:

int value = [self someMethodThatGetsAnINT];

Later on I have some "ifs" that check upon this value.

How to I express: if (value == nil)?

When I try do this intuitive code writing I get a warning saying:

Semantic Issue: Comparison between pointer and integer ('int' and 'void *')

Ohad Regev
  • 5,641
  • 12
  • 60
  • 84

3 Answers3

16

nil is just a 0 :) Try this :

if (0 == value)

However, why would you be testing nil against an int - something sounds funny here :)

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
4

If you really want a nil value returned, you should probably be using NSNumber as your return type instead of int. NSNumber is an class that wraps scalar values in objective-c. You can put an integer in it when you have a valid return value, or return nil when you don't.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
0

you can probably use if (value == (int)nil) but really a function that returns an int shouldn't return nil as nil is not an integer (although it may be represented similarly).

this may help clear it up a little

Community
  • 1
  • 1
smitec
  • 3,049
  • 1
  • 16
  • 12