4
votes

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!

3
What is type here? integer? or variable of MyURLType? - Anil Varghese
Is a variable of MyURLType - gemasr
show me the declaration of type you may use * like MyURLType *type - Anil Varghese
Yes, I am using MyURLType *type = MyURLTypeX - gemasr

3 Answers

6
votes

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)).

0
votes

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?

-1
votes

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

if (type == (MyURLType *) MyURLType2)