[英]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
). 在
MyUrlType2
和MyUrlType3
的情况下,我得到"Incompatible integer to pointer conversion"
警告(不是在MyURLType1
的情况下)。 Am I doing anything wrong in the declaration? 我在宣言中做错了吗? Any ideas?
有任何想法吗?
Thanks! 谢谢!
From your comment 从你的评论
Yes, I am using MyURLType *type = MyURLTypeX
是的,我使用的是MyURLType * type = MyURLTypeX
Then type
is not of type MyURLType
, it is of type pointer to MyURLType
. 然后
type
不是MyURLType
类型,它是pointer to MyURLType
类型的pointer to MyURLType
。
if (type == MyURLType2)
Here you are comparing a pointer type ( type
) to an integer type ( MyURLType
). 在这里,您将指针类型(
type
)与整数类型( MyURLType
)进行比较。 If the integer type is 0
it doesn't generate a warning, because it could be a check for NULL
. 如果整数类型为
0
,则不会生成警告,因为它可能是对NULL
的检查。
You either need to declare type
as a simple MyURLType
( MyURLType type =…
) or dereference type
when comparing ( if (*type == MyURLType2)
). 你要么需要声明
type
为一个简单的MyURLType
( MyURLType type =…
)或解除引用type
比较时( if (*type == MyURLType2)
why not define type as an int? 为什么不将类型定义为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. if (type == MyURLType2)
因为它们都是整数。
How is it possible no one has suggested this yet? 怎么可能没有人提出这个呢?
只是研究这个,但看起来另一种选择是施放枚举:
if (type == (MyURLType *) MyURLType2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.