<span id="Label3"></p><p><p><span style="font-size: 14px;">1. Switch parameter type</span></p></p><p><p><span style="font-size: 14px;">The switch parameter type requirement is an integer type, which, to be exact, is a type that can be converted to an integer, which includes all C basic data Types ((signed/unsigned)<span style="color: #ff6600;">char</span>, (short/long signed/ Unsigned)<span style="color: #ff6600;">int</span>, <span style="color: #ff6600;">float</span>, <span style="color: #ff6600;">double</span>, <span style="color: #ff6600;">longlong</span>, <span style="color: #ff6600;">bool</span>(c++/objective-c)), and the enumeration type, But it does not include complex types such as strings, structs, unions, objects, function pointers, and so On.</span></p></p><p><p><span style="font-size: 14px;">In fact, pointers such as String address pointers, object addresses, pointers, etc., can all be converted to UInteger because they can be interpreted as unsigned int in memory, but why can't I use these pointers as parameters to switch? This is because these pointers are dynamically specified, and each time the Compiler/program specifies a different address, this causes the case statement to be unable to determine which condition is true, whereas the conditional requirement after the case must be a constant; a reference to a complex object is often directed to a pointer, since the pointer is not determined by its Value. , so complex objects naturally cannot be used as switch parameters.</span></p></p><p><p><span style="font-size: 14px;">e.g. 1 The following code uses the NSString pointer as the switch parameter, the compilation fails, prompting statement requires expression of the integer type (' nsstring *__strong ' invalid ) Error</span></p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"> NSString *str = [nsstring stringwithformat:@ "I am a string."]; NSLog (@ "(nsinteger) str=%lx", (nsinteger) str); Switch (str) {case (nsinteger) nil: NSLog (@ "case nil"); break; Default: NSLog (@ "case default"); break; }</pre></pre><p><p><span style="font-size: 14px;"></span></p></p><p><p><span style="font-size: 14px;">E.g. 2 Modify the switch parameter (nsstring *) str to (UInteger) str after the compile pass, run no error, but do not execute any case statements</span></p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"> NSString *str = [nsstring stringwithformat:@ "I am a string."]; NSLog (@ "(nsinteger) str=%lx", (nsinteger) str); Switch ((nsinteger) str) {case (nsinteger) nil: NSLog (@ "case nil"); break; Default: NSLog (@ "case default"); break; }</pre></pre><p><p><span style="font-size: 14px;">Run again found, two run (nsinteger) str output address results are different</span></p></p><p><p><span style="font-size: 14px;">1th Time Log output</span></p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;">(nsinteger) str=1001002e0</pre></pre><p><p><span style="font-size: 14px;">2nd time Log Output</span></p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;">(nsinteger) str=1002021c0</pre></pre><p><p></p></p><p><p><span style="font-size: 14px;">This naturally does not match the case statement that is correctly determined. What if you want to match? That can only use if-else statement, can achieve more flexible effect.</span></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;"> NSString *str = [nsstring stringwithformat:@ "I am a string."]; NSString *str2 = str; if (str2 = = Str) { NSLog (@ "\nstr2 = = str"); } else{ NSLog (@ "\nstr2! = str"); }</pre></pre><p><p><span style="font-size: 14px;">Log output</span></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">STR2 = = str</pre></pre><p><p></p></p><p><p><span style="font-size: 14px;">2.case statement block attention issues</span></p></p> <ul> The <ul> <li> <span style="font-size:14px;">case statement is to match the value of a parameter in a switch, so its condition value must be constant (const). </span> </li> <li> <span style="font-size:14px;">case as a local statement block, you can also have their own variables and memory space, then declare the variables inside the case statement block, you should ensure that the variables defined within the block, not used outside the block, Failure to do so will result in a compilation error (the outside of the block is not visible to the variable); if used indirectly, It may result in a Run-time error (the memory address has been disposed, and the reference may point to an error location, known as a wild pointer). </span> </li> <li> <span style="text-decoration: underline; " if you define a new variable, need to enclose the symbol ' {} in code block, otherwise <span><span style="color: #ff0000; text-decoration:underline;" you may have an error < span> </span> during Compilation. </span></li> </ul> </ul><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;"> Switch (1) {case 1:{ int a=1; NSLog (@ "1. a=%d", a); } NSLog (@ "2. a=%d", a); <span style="color: #ff0000;">//compile error using undeclared identifier ' a '</span> int a=2; <span style="color: #ff0000;">//normal through compilation</span> NSLog (@ "3. a=%d", a); break; Case 2: int a=3; <span style="color: #ff0000;">//compile error non-expected expression</span> break; }</pre></pre><p><p></p></p><p><p>IOS switch-case know how much</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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service