{"id":51,"date":"2021-09-16T15:36:28","date_gmt":"2021-09-16T07:36:28","guid":{"rendered":"https:\/\/www.yomisoft.com\/?p=51"},"modified":"2023-09-01T10:05:01","modified_gmt":"2023-09-01T02:05:01","slug":"swift5-0%e7%b2%be%e7%ae%80%e7%ac%94%e8%ae%b0-%e7%ac%ac6%e7%ab%a0-switch-case-%e5%92%8cfallthrough%e7%a9%bf%e9%80%8f","status":"publish","type":"post","link":"https:\/\/www.yomisoft.com\/index.php\/2021\/09\/16\/swift5-0%e7%b2%be%e7%ae%80%e7%ac%94%e8%ae%b0-%e7%ac%ac6%e7%ab%a0-switch-case-%e5%92%8cfallthrough%e7%a9%bf%e9%80%8f\/","title":{"rendered":"Swift5.0\u7cbe\u7b80\u7b14\u8bb0-\u7b2c6\u7ae0-switch case \u548cfallthrough\u7a7f\u900f"},"content":{"rendered":"<p>Swift\u4e2d\u7684switch\u8bed\u53e5\u8ddf\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684switch\u8bed\u53e5\uff0c\u57fa\u672c\u4f7f\u7528\u4e0a\u4e5f\u90fd\u4e00\u6837\u3002<br \/>\n\u4e0b\u9762\u662fswitch\u8bed\u53e5\u7684\u57fa\u672c\u5f62\u5f0f\uff0c\u6ca1\u7528\u7a7f\u900f\uff0c\u5339\u914d\u5230\u54ea\u5c31\u5230\u54ea\u7ed3\u675f<\/p>\n<pre class=\"hl\"><code class=\"\">var a = 10\nswitch a {\n   case 10:\n      print(\"a = 10\")\n   case 20:\n      print(\"a = 20\")\n   default:\n      print(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\na = 10\n*\/<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u5339\u914d\u591a\u4e2a\u503c<\/strong><\/p>\n<pre class=\"hl\"><code class=\"\">let str = \"d\"\nswitch str {\n   case \"a\",\"b\",\"c\":\n      print(\"\u5339\u914da,b,c\u5176\u4e2d\u4e4b\u4e00\")\n   case \"d\",\"e\",\"f\":\n      print(\"\u5339\u914dd,e,f\u5176\u4e2d\u4e4b\u4e00\")\n   default:\n   print(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n\u5339\u914dd,e,f\u5176\u4e2d\u4e4b\u4e00\n*\/<\/code><\/pre>\n<p><strong>\u5339\u914d\u533a\u95f4\uff0c\u6d89\u53ca\u5230\u533a\u95f4\u8bed\u6cd5\uff0c\u8fd9\u91cc\u7b80\u5355\u6f14\u793a\uff0c\u540e\u9762\u4f1a\u5355\u72ec\u8bb2\u89e3\u533a\u95f4\u8bed\u6cd5<\/strong><\/p>\n<pre class=\"hl\"><code class=\"\">var a = 10\nswitch a {\ncase 1..&lt;5:\n   print(\"\u5339\u914d1\u52304\u4e4b\u95f4\u7684\u503c\")\ncase 6..&lt;11:\n   print(\"\u5339\u914d6\u523010\u4e4b\u95f4\u7684\u503c\")\ndefault:\nprint(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n\u5339\u914d6\u523010\u4e4b\u95f4\u7684\u503c\n*\/<\/code><\/pre>\n<p><strong>\u5339\u914d\u5143\u7ec4\uff0c\u4e0b\u5212\u7ebf\u53ef\u4ee5\u5339\u914d\u6240\u6709\u60c5\u51b5\uff0c\u5176\u5b9e\u5e94\u8be5\u662f\u5ffd\u7565\u4e86<\/strong><\/p>\n<pre class=\"hl\"><code class=\"\">var value = (\"hello\",10,true)\nswitch value {\n   case (\"hello\",9,true):\n      print(\"\u5339\u914d\u60c5\u51b51\")\n   case (\"world\",10,true):\n      print(\"\u5339\u914d\u60c5\u51b52\")\n   case (_,10,true):\n\/\/\u4e0b\u5212\u7ebf\u5ffd\u7565\u8be5\u503c\uff0c\u53ea\u5339\u914d\u662f\u5426\u6709\u5176\u4ed62\u4e2a\u503c\n      print(\"\u5339\u914d\u60c5\u51b53\")\n   default:\n      print(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n\u5339\u914d\u60c5\u51b53\n*\/<\/code><\/pre>\n<p><strong>\u524d\u9762\u5143\u7ec4\u7ae0\u8282\u63d0\u5230\u8fc7\uff0c\u5143\u7ec4\u53ef\u4ee5\u5c06\u4e34\u65f6\u8d4b\u7ed9\u53d8\u91cf\u6216\u5e38\u91cf\uff0cswitch\u4e5f\u53ef\u4ee5\u4f7f\u7528<\/strong><\/p>\n<pre class=\"hl\"><code class=\"\">var value = (10,20)\nswitch value {\n   case let (name1,name2):\n      print(\"name1 = \\(name1)\")\n      print(\"name2 = \\(name2)\")\n   case var (10,name3):\n      print(\"name3 = \\(name3)\")\n   default:\n      print(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\nname1 = 10\nname2 = 20\n*\/<\/code><\/pre>\n<p><strong>\u7528where\u6765\u5224\u65ad\u989d\u5916\u60c5\u51b5\uff0c\u4fee\u6539\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u52a0\u5165where\u6765\u5224\u65ad\u989d\u5916\u60c5\u51b5\uff0cname1&gt;name2 \u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u6240\u4ee5\u5c31\u7ee7\u7eed\u5f80\u4e0b\u5339\u914d<\/strong><\/p>\n<pre class=\"hl\"><code class=\"\">var value = (10,20)\nswitch value {\n   case let (name1,name2) where name1 &gt; name2:\n      print(\"name1 = \\(name1)\")\n      print(\"name2 = \\(name2)\")\n   case var (10,name3):\n      print(\"name3 = \\(name3)\")\n   default:\n      print(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\nname3 = 20\n*\/<\/code><\/pre>\n<p><strong>\u5982\u679c\u60f3\u8981\u8d2f\u7a7f\u6548\u679c\uff0c\u90a3\u4e48\u4f7f\u7528fallthrough\u5173\u952e\u5b57<\/strong><\/p>\n<pre class=\"hl\"><code class=\"\">var value = 10\nswitch value {\n   case 10:\n      print(\"\u5339\u914d\u60c5\u51b51\")\n      fallthrough\n   case 20:\n      print(\"\u5339\u914d\u60c5\u51b52\")\n   default:\n      print(\"\u5176\u4ed6\u60c5\u51b5\")\n}\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n\u5339\u914d\u60c5\u51b51\n\u5339\u914d\u60c5\u51b52\n*\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Swift\u4e2d\u7684switch\u8bed\u53e5\u8ddf\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684switch\u8bed\u53e5\uff0c\u57fa\u672c\u4f7f\u7528\u4e0a\u4e5f\u90fd\u4e00\u6837\u3002 \u4e0b\u9762\u662fswitch\u8bed\u53e5\u7684\u57fa\u672c\u5f62\u5f0f\uff0c\u6ca1\u7528\u7a7f\u900f\uff0c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":70,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[8,10,12],"class_list":["post-51","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3","tag-swift","tag-10","tag-12"],"_links":{"self":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":1,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions\/76"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/media\/70"}],"wp:attachment":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}