如何创建“或”开关使用typedef枚举? [英] How do I create "or'ed" switches using typedef enum?
问题描述
我有点像枚举
。他们的语法是神秘的,我还没有找到一个确定的参考点,他们无可否认地正确使用..但让我们说我有..
I kinda like enums
. Their syntax is arcane-looking, and I've yet to find a definitive point of reference on their undeniably proper use.. but let's say I had..
typedef enum {
OrientTop,
OrientBottom,
OrientFiesta
} Orient;
我喜欢像我一样,像多个选择可能适用/需要,只是做...
I'd love to be able, as I do with, for example, other constants when multiple chocies may be applicable/ required, just do…
self.orientation = OrientTop | OrientFiesta; // NO NO WORK-O!
就像一个人一样...
just like one does with…
self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
还是..
it = [[NSThing alloc]initOptions: NSStupid | NSSpicy | NSSassy];
它也会很好..而不是...
and it would also be nice.. instead of…
if ((o == OrientTop ) || ( o == OrientBottom))
我只能使用...
if (o == OrientTop || OrientBottom)
最重要的是...如何检查多个案例,ála ..
switch (orientation) {
case OrientTop | OrientBottom:
或类似的东西..
呃,你们太慢了..所以,我只需要...
Or something, of the sort..
ugh, oh.. you guys are too slow.. so, duh.. i just need to…
case OrientLeft:
case OrientRight: { // blah blah blah
break; }
(但是第一部分..)苹果/比我更聪明的人...正在使用他们的 typedef
的额外的zing,使我的味道这样,ech..bland ..相比之下
(But for the first part.. ) What is the extra "secret sauce" that Apple / smarter people than me.. are using to give their typedef
's that extra zing that make mine taste so, ech..bland.. in comparison?
推荐答案
看起来好像 typedef
如 NSViewWidthSizable
实际上是bitmasks,这允许你喜欢的很好的ORing操作。在
UIKit
元素的标题中,您可以看到我的意思:
It appears as if typedef
s such as NSViewWidthSizable
are actually bitmasks, which allow the nice ORing operations you so enjoy. In the headers for some UIKit
elements, you can see what I mean:
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;
我从非常有用的网站,更详细的解释,但你也可以通过保持命令⌘来检查任何常数的枚举,单击常量或typedef。
I snatched that right off a very helpful site that explains in more detail, but you can also examine the enumeration of any constant by holding Command ⌘ and clicking the constant or typedef.
这篇关于如何创建“或”开关使用typedef枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!