ios - typedef NS_OPTIONS check like UIViewAutoresizing

ID : 274533

viewed : 29

Tags : iostypedefios





Top 4 Answer for ios - typedef NS_OPTIONS check like UIViewAutoresizing

vote vote

91

The right way to check your bitmask is by decoding the value using the AND (&) operator as follows:

Arrows a = (ArrowLeft | ArrowRight);     if (a & ArrowBottom) {    NSLog(@"arrow bottom code here");     }  if (a & ArrowLeft) {    NSLog(@"arrow left code here");     }      if (a & ArrowRight) {    NSLog(@"arrow right code here");     }  if (a & ArrowTop) {    NSLog(@"arrow top code here");     } 

This will print out in the console:

arrow left code here arrow right code here 
vote vote

85

To do a compound check you could use the following code:

if ((a & ArrowRight) && (a & ArrowTop)) {        NSLog(@"arrow right and top code here");     } 
vote vote

74

The correct way to check for this value is to first bitwise AND the values and then check for equality to the required value.

Arrows a = (ArrowBottom | ArrowLeft);     if (((a & ArrowBottom) == ArrowBottom) && ((a & ArrowLeft) == ArrowLeft)) {     // arrow bottom-left } 

The following reference explains why this is correct and provides other insights into enumerated types.

Reference: checking-for-a-value-in-a-bit-mask

vote vote

70

Top 3 video Explaining ios - typedef NS_OPTIONS check like UIViewAutoresizing







Related QUESTION?