2010-09-04 153 views
1

好了,所以我定義我的DSNavigationManager類,它有一個名爲DSNavigationManagerStyle managerStyle屬性:iPhone運營商

 

typedef enum { 
    DSNavigationManagerStyleNone      = 0, 
    DSNavigationManagerStyleDefaultNavigationBar  = 1 << 0, 
    DSNavigationManagerStyleDefaultToolBar    = 1 << 1, 
    DSNavigationManagerStyleDefault      = 
     DSNavigationManagerStyleDefaultNavigationBar + 
     DSNavigationManagerStyleDefaultToolBar, 
    DSNavigationManagerStyleInteractiveNavigationBar = 1 << 2, 
    DSNavigationManagerStyleInteractiveToolBar   = 1 << 3, 
    DSNavigationManagerStyleInteractiveWithDarkPanel = 1 << 4, 
    DSNavigationManagerStyleInteractiveWithBackButton = 1 << 5, 
    DSNavigationManagerStyleInteractiveWithTitleBar  = 1 << 6, 
    DSNavigationManagerStyleInteractiveDefault   = 
     DSNavigationManagerStyleInteractiveNavigationBar + 
     DSNavigationManagerStyleInteractiveToolBar + 
     DSNavigationManagerStyleInteractiveWithDarkPanel + 
     DSNavigationManagerStyleInteractiveWithBackButton + 
     DSNavigationManagerStyleInteractiveWithTitleBar, 
} DSNavigationManagerStyle; 

 

我只是學會了如何使用逐位移,但我不知道如何獲得這些信息。我想要做的事有點像:

 


DSNavigationManagerStyle managerStyle = DSNavigationManagerStyleDefault; 

if(managerStyle "Has The DefaultNavigationBar bit or the DefaultToolBarBit") { 
    // Implement 
} 
else { 
    if(managerStyle "Has the InteractiveNavigationBar bit") { 
     // Implement 
    } 
    if(managerStyle "Has the InteractiveToolBar bit") { 
     // Implement 
    } 
    //.... and so on so that technically the object can implement all 
    // styles, no styles, or any number of styles in between 

} 


 

回答

2
if (managerStyle & DSNavigationManagerStyleDefaultNavigationBar || managerStyle & DSNavigationManagerStyleDefaultToolBarBit) { 
    // Implement 
} else if (managerStyle & DSNavigationManagerStyleInteractiveNavigationBar) { 
    // Implement 
} else if (managerStyle & DSNavigationManagerStyleInteractiveToolBar) { 
    // Implement 
} 
    //.... and so on so that technically the object can implement all 
    // styles, no styles, or any number of styles in between 
} 

&是按位與運算。您應該閱讀關於Bitwise operation的維基百科文章。

1

要檢查是否存在某個特定位,請使用按位和&(不要與&&,邏輯和)混淆。例如,

01101101 
& 00001000 
---------- 
    00001000 

如果您使用此值它將被轉換爲布爾值,任何非零值被認爲是「真」,這使得這樣容易測試閱讀:

if (managerStyle & DSNavigationManagerStyleDefaultToolBar) { 
    ... 
} 

但是這個測試不適合你的複合值 - 例如,使用DSNavigationManagerStyleDefault的位域將返回'真',如果任何組件位被設置。

如果您確實想使用位域,請使用所有的按位運算符:http://developer.apple.com/tools/mpw-tools/commandref/appc_logical.html