Enumeration is often used in the development, can represent a state of several kinds of different things or type, etc., so we usually according to the current status and the enumeration element compared to get what is the current status, but I found a problem, the comparison of enumeration values are really we give enumeration assign values to simply compare equal size or whether to come to the conclusion? Without further ado, look at the code:




Define one of these enumerations for the moment and assign values separately

Then let’s do the simplest if judgment and print the result:




Here you can guess the result, haha

The results can be awkward, as follows:




Doubt the correctness of the can knock ha, code is not much


Why did this happen? Let’s define another enumeration, but this time assign to a single enumeration using bitwise operations (which is what Apple and many SDKS use), as follows:




And I wrote the binary numbers just to make it easier to understand

Then do the same assignment and add the if judgment, and the result is as follows:









This is the result. Do you see anything?

The explanation is as follows:




This is the process of comparing multiple state enumerated values, and the final output is KTextOnly and KHUDWithText

Summary: Comparisons of enumerations are not simply comparisons of values, but comparisons of bits. When does this situation come into play? For example, if a person at the same time satisfy the two conditions is the boss and a man, this time using HttpHUDType type = KTextOnly | KHUDWithText; If you simply assign constants to enumerations as in the first example (I used to assign constants 🙃 before discovering this problem), you can make a mistake. This problem was also discovered by accident today. What say is wrong or improper place welcomes everybody to criticize and correct 😄. [NS_OPTIONS] here’s a tip: We usually can use a console project as an “experimental project” on the desktop, usually have any questions unrelated to the UI can be directly in the project interview, because the console program runs faster will not take up too much time