可以向现有枚举类型添加另一个项目吗?
我想知道是否可以向现有枚举类型(框架的一部分)添加/附加另一个项目?
像这样的东西:我们有 enum
类型
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
现在我想向此集合追加或添加一个像 UIModalTransitionStyleCoverVerticalFlipped
的项目。 这样的事情能完成吗?
I wonder whether it is possible to add/append another item to an existing enum type (part of a framework)?
Something like this: We have the enum
type
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
Now I want to append or add to this set an item like UIModalTransitionStyleCoverVerticalFlipped
.
Can something like this be accomplished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以强制新元素与枚举具有相同的类型,但不能在子类中扩展它。
头文件:
实现文件:
确保留出一些空间,以防框架扩展,这样就不会发生冲突。这有点破解,但它会消除编译器错误和警告。
You can force new element to have the same type as the enum, but you can't extend it in a subclass.
header file:
implementation file:
Make sure to give some space in case the framework is extended, so that you don't have conflicts. This is a bit of a hack, but it will get rid of compiler errors and warnings.
为此,您必须修改原始类型定义以包含新值:
否则,您可以冒险它不起作用,并单独定义它:
可以保存原始枚举的变量通常 当/如果您也分配新值时(在典型情况下,它只是一个
int
),也可以很好地工作 - 但它不能保证。至少在理论上,实现可以/可以分配足够少的位来保存该枚举,以这种方式添加更多值是行不通的。它还可以进行范围检查,因此不允许分配任何超出范围的值。这些都不常见,所以从实践的角度来看,这可能不是问题——但从理论的角度来看,没有什么能真正保证这样的代码能够工作。To do it, you have to modify the original type definition to include the new value:
Otherwise, you can take a chance on its not working, and define it separately:
A variable that could hold the original enumeration will usually also work perfectly fine when/if you assign the new value as well (in a typical case, it'll just be an
int
) -- but it's not guaranteed. At least in theory, the implementation can/could assign few enough bits to hold that enumeration that it adding more values this way wouldn't work. It could also do range checking so assigning any out of range value wouldn't be allowed. Neither of these is at all common, so from a practical viewpoint it's probably not a problem -- but from a theoretical viewpoint, nothing really guarantees that code like this will work.也许这可以帮助您:
现在您可以打开 SubType,知道这些值是唯一的。
如果您无权访问 BaseType,则可以将 SubTypeCase1 设置为 BaseType 的最后一项 + 1。
缺点是,您无法声明采用 SubType 并向其传递 BaseType 的方法,而不会收到编译器警告。因此,您需要声明采用 NSIntegers 的方法,以便消除该警告。
另外,当您需要声明 SubType 的参数并能够传入 BaseType 时,感觉很奇怪。
Maybe this can help you:
Now you can switch on SubType knowing the values are unique.
If you don't have access to BaseType, you could set SubTypeCase1 to BaseType's last item + 1.
Downside is, you can't declare a method that takes a SubType and pass to it a BaseType without getting a compiler warning. So you need to declare your methods to take NSIntegers in order silence that warning.
Also, it feels weird when you need to declare a parameter of SubType and be able to pass in a BaseType.
为此,您必须更新 Enum 声明以包含
UIModalTransitionStyleCoverVerticalFlipped
这个值,这样,UIModalTransitionStyleCoverVerticalFlipped
都将等效于整数常量 4无论您使用 Enum dec 中的任何字符串常量, 。相应的常量值被替换,因此它用于约束变量在上述情况下仅保存指定的值集(即 0 到 4)
To do this you have to update the Enum declaration to include
UIModalTransitionStyleCoverVerticalFlipped
this values as wellso
UIModalTransitionStyleCoverVerticalFlipped
will be equivalent to integer constant 4whereever you use any string constant from Enum dec. corresponding constant value get replaced so it is used to constraint the variable to hold only specified set of values only(i.e. 0 to 4) in above mentioned case