gemasr
gemasr

Reputation: 187

Warning: Incompatible integer to pointer conversion when using NS_ENUM types

I am using an enum, something like this:

typedef NS_ENUM(NSInteger, MyURLType) {
    MyURLType1,
    MyURLType2,
    MyURLType3
};

The problem appears when I try to compare or identify the type:

if (type == MyURLType2)

I am getting a "Incompatible integer to pointer conversion" warning in the case of MyUrlType2 and MyUrlType3 (not in the case of MyURLType1). Am I doing anything wrong in the declaration? Any ideas?

Thanks!

Upvotes: 4

Views: 3961

Answers (3)

jungledev
jungledev

Reputation: 4335

why not define type as an int? Then, you can compare the ints. Simple and clean solution.

int type = MyURLTypeX; 

will allow you to do

if (type == MyURLType2) since they're both ints.

How is it possible no one has suggested this yet?

Upvotes: 0

evliu
evliu

Reputation: 504

was just researching this, but looks like another option is to cast the enum:

if (type == (MyURLType *) MyURLType2)

Upvotes: -1

Sebastian
Sebastian

Reputation: 7710

From your comment

Yes, I am using MyURLType *type = MyURLTypeX

Then type is not of type MyURLType, it is of type pointer to MyURLType.

if (type == MyURLType2)

Here you are comparing a pointer type (type) to an integer type (MyURLType). If the integer type is 0 it doesn't generate a warning, because it could be a check for NULL.

You either need to declare type as a simple MyURLType (MyURLType type =…) or dereference type when comparing (if (*type == MyURLType2)).

Upvotes: 6

Related Questions