之前遇到過這樣一個(gè)問題,在一個(gè)先前設(shè)計(jì)好的枚舉類型中添加或刪除一個(gè)枚舉值(刪除是不可能的,這輩子都不可能的诺祸,最多只能是棄用),或組合一些枚舉值祭芦,要如何做才能看起來不那么尬筷笨。
感到尬的原因可能只是當(dāng)時(shí)設(shè)計(jì)的時(shí)候考慮的不夠全面,也或許是這個(gè)枚舉類型已經(jīng)無法滿足你的需求了龟劲,這時(shí)候就要換個(gè)思路胃夏,可以像蘋果這樣:
這是iOS6之前使用的:
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
這是在iOS6之后添加的:
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;
先是對(duì)先前存在的枚舉值進(jìn)行了一個(gè)等價(jià)的平移,之后對(duì)每個(gè)值都進(jìn)行了按位左移昌跌,這個(gè)的目的在于為新增的枚舉值添加便利仰禀,可以通過按位或來實(shí)現(xiàn)。
真是一舉兩得蚕愤。