简体   繁体   中英

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!

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

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?

只是研究这个,但看起来另一种选择是施放枚举:

if (type == (MyURLType *) MyURLType2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM