Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an enum variable null?

I'm new to Objective C. Here's my code

enum effectName {
    RESET, Flea,Tint,Grayscale,Pixelate,Blur,Mirror,changeBrightness,changeContrast,Invert
};
enum effectName EffectToApply;        
enum effectName prevEffect;

and in the implementation file, I've to declare EffectToApply and prevEffect as null. I tried doing it like this

-(void) Effect    
{        
     EffectToApply= NULL;        
     prevEffect=nil;
} 

but it gives a warning Incompatible pointer to integer conversion. Can someone help me how to do this?

like image 358
user3863537 Avatar asked Mar 19 '23 18:03

user3863537


1 Answers

An enum is effectively an integer and, like an integer, such a type cannot represent a NULL value discreet from 0 in either C or Objective-C1

The simplest way to deal with this is to have a "NONE" enum option, generally with the value 0.

enum effectName {
    NoEffect, ResetEffect, ..
};

enum effectName EffectToApply; // default value => 0 / NoEffect

1 See Can a conforming C implementation #define NULL to be something wacky for a lot of C-lawyer speak wrt how NULL is defined; some libraries define NULL as 0 or 0L, which while permissible would (unfortunately) not have emitted the observed warnings.

like image 161
user2864740 Avatar answered Apr 05 '23 22:04

user2864740