objective c - Detect if NSNumber is zero, nil or 0 -


i have variable in core data. want detect cases nil zero, null or otherwise not have nice value such 222 or 333.

this should trivial getting caught in objective-c's syntax.

following code not working:

if (_item.id!=nil && _item.id!=0) { //do } 

of note id should nsnumber.

it defined as

@property (nonatomic, retain) nsnumber * id; 

i should clarify not working when value logs console 0.

given way variable types , core data work, cannot tell causes variable log console '0' causing so. basically, want exclude cases value other non-zero integer (in mathematical, not computer science terms).

to check numeric value stored in nsnumber, have call 1 of methods give primitive type.

e.g. integervalue, unsignedlonglongvalue, doublevalue

to correctly check nil , value of 0, need following:

if (_item.id != nil && [_item.id intvalue] != 0) {     // code here } 

because sending message nil reference returns 0, can take shortcut:

if ([_item.id intvalue] != 0) ... 

this works because _item.id has non-nil return non-zero value intvalue.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -