<span id="Label3"></p><p><p><strong>Introduction:</strong></p></p><p><p>Enumeration <strong>Value</strong> It is an <strong>integer (int)</strong> and, It is not involved in memory consumption and deallocation <strong>,</strong> The enumeration definition variable can be used directly without Initialization.</p></p><p><p>There is only one purpose of using enumerations in code, which is to increase the readability of the Code.</p></p><p><p></p></p><p><p><strong>Use:</strong></p></p><p><p>the <strong>enumeration</strong> is defined as Follows:</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt">typedef <span class="keyword">ENUM</span></li> <li>{</li> <li class="alt"><span class="comment">//the Following is an enumeration member</span></li> <li>TestA = 0,</li> <li class="alt">testb,</li> <li>testc,</li> <li class="alt">TestD</li> <li>}test; <span class="comment">//enumeration name</span></li> </ol> </ol><p><p><br>can also be defined as follows ( <strong>recommended: the structure is clearer</strong> ):</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt">typedef ns_enum (nsinteger, Test1)</li> <li>{</li> <li class="alt"><span class="comment">//the Following is an enumeration member</span></li> <li>TEST1A = 0,</li> <li class="alt">TEST1B = 1,</li> <li>test1c = 2,</li> <li class="alt">TEST1D = 3</li> <li>};</li> </ol> </ol><p><p><br>The definition of an enumeration also supports the way in which <strong>bit operations</strong> are defined, as Follows:</p></p><p><p>The equals sign must be equal to 1 after</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <li class="alt"><li class="alt">typedef ns_enum (nsinteger, Test)</li></li> <li><li>{</li></li> <li class="alt"><li class="alt">TestA = 1, <span class="comment">//1 1 1</span></li></li> <li><li>TESTB = 1 << 1, <span class="comment">//2 2 10 converted to 10 binary 2</span></li></li> <li class="alt"><li class="alt">TESTC = 1 << 2, <span class="comment">//4 3 100 converted to 10 binary 4</span></li></li> <li><li>TestD = 1 << 3, <span class="comment">//8 4 1000 converted to 10 Binary 8</span></li></li> <li class="alt"><li class="alt">Teste = 1 << 4 <span class="comment">//16 5 10000 converted to 10-in</span></li></li> <li><li>};</li></li> </ol><p><p>When do you want to use this method?</p></p><p><p>That's when an enumeration variable might represent multiple enumeration values. In fact, when assigning multiple enumeration values to an enumeration variable, the principle simply adds up the individual enumeration Values.</p></p><p><p>When you add up, you get a new value, and in order to ensure the uniqueness of this value, this time it is the important function of the bit Operation.</p></p><p><p>Bitwise operations ensure uniqueness of the combination of enumeration Values.</p></p><p><p>Because the bitwise operation is computed by converting the binary to decimal, that is, the computed decimal value is accessed in the enumeration Value.</p></p><p><p>To make an analogy:</p></p><p><p>After the enumeration is set by the bitwise operation above, the printed enumeration values Are: 1 2 4 8 16</p></p><p><p>These 5 numbers, no matter how you combine them, don't produce two of the same numbers.</p></p><p><p>Manually to create a bit operation enumeration, and a little bit of time, Fortunately Apple has prepared a <strong>uint</strong>for Us. so, initialize a bitwise operation enumeration in this way:</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt">typedef ns_enum (<span class="keyword">uint, Test)</span></li> <li>{</li> <li class="alt">TestA,</li> <li>testb,</li> <li class="alt">testc,</li> <li>TestD,</li> <li class="alt">Teste</li> <li>};</li> </ol> </ol><p><p><br><br></p></p><p><p></p></p><p><p></p></p><p><p>Multiple enumeration values are assigned as Follows:</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt">Test tes = (testa| testb);</li> </ol> </ol><p><p><br>To determine if an enumeration variable contains a fixed enumeration value, you need to ensure the enumeration values and the uniqueness of each combination before use:</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <li class="alt"><li class="alt">NSLog (@ "%d%d%d%d%d<span class="string">", testa,testb,testc,testd,teste); </span></li></li> <li><li>Test tes = (testa| testb);</li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"%d", tes); </span></li></li> <li><li>NSLog (@<span class="string">"%d", (tes & TestA)); </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">If (tes & TestA)) {</span></li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"there"); </span></li></li> <li><li>}<span class="keyword">Else</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li>NSLog (@<span class="string">"no"); </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>NSLog (@<span class="string">"%d", (tes & testb)); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">If (tes & TestA)) {</span></li></li> <li><li>NSLog (@<span class="string">"there"); </span></li></li> <li class="alt"><li class="alt">}<span class="keyword">Else</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"no"); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>NSLog (@<span class="string">"%d", (tes & testc)); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">If (tes & testc)) {</span></li></li> <li><li>NSLog (@<span class="string">"there"); </span></li></li> <li class="alt"><li class="alt">}<span class="keyword">Else</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"no"); </span></li></li> <li><li>}</li></li> </ol><p><p>If not included, returns 0, 0 indicates false no then goes to else<br><br></p></p><p><p>It is also possible to accumulate a value for an enumeration variable at any time, but to control not to add an enumerated value that has already been added, the value of the enumeration variable will not change, but it will mislead the person reading the Code</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt">TES |=testc;</li> </ol> </ol><p><p></p></p><p><p>There is accumulation, naturally have to lose, if the decrement of the enumeration value does not exist, then this time the decrement of the enumeration value will be automatically added up.</p></p><strong><strong>[csharp]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt">tes^= teste;</li> </ol> </ol> </ol><p><p>iOS enumeration displacement operations</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.