是否可以在C / Objective-C中为枚举创建别名?

时间:2014-03-04 15:29:26

标签: objective-c c

示例:

我发现枚举UIViewAnimationCurve太长了。我想写CurveEaseIn

所以我试图创建一个别名:

typedef NS_ENUM(NSInteger, CurveEase) {
    CurveEaseInOut = UIViewAnimationCurveEaseInOut,
    CurveEaseIn = UIViewAnimationCurveEaseIn,
    CurveEaseOut = UIViewAnimationCurveEaseOut
};

当我在需要指定UIViewAnimationCurve的地方使用我的新枚举时,我必须抛出它,否则Xcode会抛出警告:

Implicit conversion from enumeration type 'enum CurveEase' to different enumeration type 'UIViewAnimationCurve' (aka 'enum UIViewAnimationCurve')

我也尝试过:

typedef NS_ENUM(UIViewAnimationCurve, CurveEase) {
    CurveEaseInOut = UIViewAnimationCurveEaseInOut,
    CurveEaseIn = UIViewAnimationCurveEaseIn,
    CurveEaseOut = UIViewAnimationCurveEaseOut
};

注意UIViewAnimationCurve作为枚举的类型。这给出了错误:

Non-integral type 'UIViewAnimationCurve' (aka 'enum UIViewAnimationCurve') is an invalid underlying type

所以看起来我坚持使用NSInteger作为类型。但有没有办法欺骗编译器接受CurveEase而不进行强制转换,并且没有抑制警告?

1 个答案:

答案 0 :(得分:3)

typedef UIAnimationCurve CurveEase怎么样?