2012-07-30 64 views
1

我有點像enums。其語法是神祕的,看的,我還沒有找到對自己不可否認正確使用引用一個明確的點..但讓我們說我有..如何使用typedef枚舉創建「or'ed」開關?

typedef enum { 
    OrientTop, 
    OrientBottom, 
    OrientFiesta 
} Orient; 

我很想能,因爲我做,例如,其他常量當多個chocies可以適用/必需的,只是做...

self.orientation = OrientTop | OrientFiesta; // NO NO WORK-O! 

就像一個用幹...

self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; 

或也..

it = [[NSThing alloc]initOptions: NSStupid | NSSpicy | NSSassy]; 

,它也將是不錯的..而不是...

if ((o == OrientTop) || (o == OrientBottom)) 

我可以只使用...

if (o == OrientTop || OrientBottom) 

,最重要的...如何檢查多個的情況下,A LA。 。

switch (orientation) { 
    case OrientTop | OrientBottom: 

什麼的,那種.. 呃,哦..你們是太慢了..所以,廢話..我只需要...

case OrientLeft: 
case OrientRight: { // blah blah blah 
    break;  } 

(但對於第一部分..)什麼是蘋果/聰明人比我更多的「祕訣」是用來給他們的typedef的額外zing,使我的味道如此,相當於......相比之下呢?

回答

3

看起來好像typedefNSViewWidthSizable實際上是位掩碼,它允許你喜歡的很好的ORing操作。在標題爲一些UIKit元素,你可以明白我的意思:

enum { 
    UIViewAutoresizingNone     = 0, 
    UIViewAutoresizingFlexibleLeftMargin = 1 << 0, 
    UIViewAutoresizingFlexibleWidth  = 1 << 1, 
    UIViewAutoresizingFlexibleRightMargin = 1 << 2, 
    UIViewAutoresizingFlexibleTopMargin = 1 << 3, 
    UIViewAutoresizingFlexibleHeight  = 1 << 4, 
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5 
}; 
typedef NSUInteger UIViewAutoresizing; 

我搶走這一權利掀起了very helpful site that explains in more detail,但你也可以檢查任何常量的枚舉通過持有命令⌘並單擊不變或typedef。

+1

添加:祕訣就是C. Objective-C是C語言,帶有Smalltalk風格的消息傳遞,但它仍然是C,有很多C.請參閱? – 2012-07-30 19:42:57

+0

Woa ... Command⌘真的很酷。 – Almo 2012-07-30 21:27:43

+0

哇,你鏈接到的網頁是:完全相關的,b:心靈麻木的詳細,c:令人震驚的難以理解,儘管它很有意識地試圖簡化主題。讓我只是把我的頭,我會回來,哈哈。 – 2012-08-01 02:35:37