user3863537
user3863537

Reputation: 111

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?

Upvotes: 5

Views: 5101

Answers (1)

user2864740
user2864740

Reputation: 61975

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.

Upvotes: 7

Related Questions