{"id":520,"date":"2018-02-23T23:38:57","date_gmt":"2018-02-23T15:38:57","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-switch-%e8%af%ad%e5%8f%a5\/"},"modified":"2018-02-23T23:38:57","modified_gmt":"2018-02-23T15:38:57","slug":"swift-switch-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=520","title":{"rendered":"Swift switch \u8bed\u53e5"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift <span class=\"color_h1\">switch \u8bed\u53e5<\/span><\/h1>\n<p><a href=\"\/swift-decision-making.html\"><img decoding=\"async\" class=\"navup\" src=\"\/images\/up.gif\" alt=\"Swift \u6761\u4ef6\u8bed\u53e5\">Swift \u6761\u4ef6\u8bed\u53e5<\/a><\/p>\n<p><b>switch<\/b> \u8bed\u53e5\u5141\u8bb8\u6d4b\u8bd5\u4e00\u4e2a\u53d8\u91cf\u7b49\u4e8e\u591a\u4e2a\u503c\u65f6\u7684\u60c5\u51b5\u3002 Swift \u8bed\u8a00\u4e2d\u53ea\u8981\u5339\u914d\u5230 case \u8bed\u53e5\uff0c\u5219\u6574\u4e2a switch \u8bed\u53e5\u6267\u884c\u5b8c\u6210\u3002<\/p>\n<hr>\n<h2>\u8bed\u6cd5<\/h2>\n<p>Swift \u8bed\u8a00\u4e2d switch \u8bed\u53e5\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre>\nswitch expression {\n   case expression1  :\n      statement(s)\n      fallthrough \/* \u53ef\u9009 *\/\n   case expression2, expression3  :\n      statement(s)\n      fallthrough \/* \u53ef\u9009 *\/\n  \n   default : \/* \u53ef\u9009 *\/\n      statement(s);\n}\n<\/pre>\n<p>\u4e00\u822c\u5728 switch \u8bed\u53e5\u4e2d\u4e0d\u4f7f\u7528 fallthrough \u8bed\u53e5\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u6ce8\u610f case \u8bed\u53e5\u4e2d\u5982\u679c\u6ca1\u6709\u4f7f\u7528 <b>fallthrough<\/b> \u8bed\u53e5\uff0c\u5219\u5728\u6267\u884c\u5f53\u524d\u7684 case \u8bed\u53e5\u540e\uff0cswitch \u4f1a\u7ec8\u6b62\uff0c\u63a7\u5236\u6d41\u5c06\u8df3\u8f6c\u5230 switch \u8bed\u53e5\u540e\u7684\u4e0b\u4e00\u884c\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u4e86<b>fallthrough<\/b> \u8bed\u53e5\uff0c\u5219\u4f1a\u7ee7\u7eed\u6267\u884c\u4e4b\u540e\u7684 case \u6216 default \u8bed\u53e5\uff0c\u4e0d\u8bba\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n<blockquote>\n<p><b>\u6ce8\u610f\uff1a<\/b>\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d\uff0cswitch \u8bed\u53e5\u5757\u4e2d\uff0ccase \u8981\u7d27\u8ddf break\uff0c\u5426\u5219 case \u4e4b\u540e\u7684\u8bed\u53e5\u4f1a\u987a\u5e8f\u8fd0\u884c\uff0c\u800c\u5728 Swift \u8bed\u8a00\u4e2d\uff0c\u9ed8\u8ba4\u662f\u4e0d\u4f1a\u6267\u884c\u4e0b\u53bb\u7684\uff0cswitch \u4e5f\u4f1a\u7ec8\u6b62\u3002\u5982\u679c\u4f60\u60f3\u5728 Swift \u4e2d\u8ba9 case \u4e4b\u540e\u7684\u8bed\u53e5\u4f1a\u6309\u987a\u5e8f\u7ee7\u7eed\u8fd0\u884c\uff0c\u5219\u9700\u8981\u4f7f\u7528 fallthrough \u8bed\u53e5\u3002<\/p>\n<\/blockquote>\n<h3>\u5b9e\u4f8b1<\/h3>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6ca1\u6709\u4f7f\u7528 fallthrough \u8bed\u53e5\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar index = 10\n\nswitch index {\n   case 100  :\n      print( \"index \u7684\u503c\u4e3a 100\")\n   case 10,15  :\n      print( \"index \u7684\u503c\u4e3a 10 \u6216 15\")\n   case 5  :\n      print( \"index \u7684\u503c\u4e3a 5\")\n   default :\n      print( \"\u9ed8\u8ba4 case\")\n}\n<\/pre>\n<p>\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n<pre>\nindex \u7684\u503c\u4e3a 10 \u6216 15\n<\/pre>\n<h3>\u5b9e\u4f8b2<\/h3>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4f7f\u7528 fallthrough \u8bed\u53e5\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar index = 10\n\nswitch index {\n   case 100  :\n      print( \"index \u7684\u503c\u4e3a 100\")\n      fallthrough\n   case 10,15  :\n      print( \"index \u7684\u503c\u4e3a 10 \u6216 15\")\n      fallthrough\n   case 5  :\n      print( \"index \u7684\u503c\u4e3a 5\")\n   default :\n      print( \"\u9ed8\u8ba4 case\")\n}\n<\/pre>\n<p>\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n<pre>\nindex \u7684\u503c\u4e3a 10 \u6216 15\nindex \u7684\u503c\u4e3a 5\n<\/pre>\n<p><a href=\"\/swift-decision-making.html\"><img decoding=\"async\" class=\"navup\" src=\"\/images\/up.gif\" alt=\"Swift \u6761\u4ef6\u8bed\u53e5\">Swift \u6761\u4ef6\u8bed\u53e5<\/a><\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift switch \u8bed\u53e5 Swift \u6761\u4ef6\u8bed\u53e5 switch \u8bed\u53e5\u5141\u8bb8\u6d4b\u8bd5\u4e00\u4e2a\u53d8\u91cf\u7b49&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=520\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,28],"tags":[74,119],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-mobile","category-swift","tag-ios","tag-swift","zoom-theme-has-thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift switch \u8bed\u53e5 - \u8c31\u5b50\u68cb - Puzi7<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.puzi7.net\/?p=520\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift switch \u8bed\u53e5 - \u8c31\u5b50\u68cb - Puzi7\" \/>\n<meta property=\"og:description\" content=\"Swift switch \u8bed\u53e5 Swift \u6761\u4ef6\u8bed\u53e5 switch \u8bed\u53e5\u5141\u8bb8\u6d4b\u8bd5\u4e00\u4e2a\u53d8\u91cf\u7b49&hellip;Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.puzi7.net\/?p=520\" \/>\n<meta property=\"og:site_name\" content=\"\u8c31\u5b50\u68cb - Puzi7\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-23T15:38:57+00:00\" \/>\n<meta name=\"author\" content=\"\u77f3\u8863\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u77f3\u8863\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520\"},\"author\":{\"name\":\"\u77f3\u8863\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#\\\/schema\\\/person\\\/87dd4941c8ec260c1350b7f41980a190\"},\"headline\":\"Swift switch \u8bed\u53e5\",\"datePublished\":\"2018-02-23T15:38:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520\"},\"wordCount\":33,\"commentCount\":0,\"keywords\":[\"iOS\",\"Swift\"],\"articleSection\":[\"Mobile\",\"Swift\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.puzi7.net\\\/?p=520#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520\",\"url\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520\",\"name\":\"Swift switch \u8bed\u53e5 - \u8c31\u5b50\u68cb - Puzi7\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#website\"},\"datePublished\":\"2018-02-23T15:38:57+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#\\\/schema\\\/person\\\/87dd4941c8ec260c1350b7f41980a190\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.puzi7.net\\\/?p=520\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=520#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6280\u672f\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/?cat=9\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Mobile\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/?cat=26\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Swift\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/?cat=28\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Swift switch \u8bed\u53e5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#website\",\"url\":\"https:\\\/\\\/www.puzi7.net\\\/\",\"name\":\"\u8c31\u5b50\u68cb - Puzi7\",\"description\":\"\u5206\u4eab\uff0c\u6210\u957f\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.puzi7.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#\\\/schema\\\/person\\\/87dd4941c8ec260c1350b7f41980a190\",\"name\":\"\u77f3\u8863\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g\",\"caption\":\"\u77f3\u8863\"},\"sameAs\":[\"https:\\\/\\\/www.puzi7.net\"],\"url\":\"https:\\\/\\\/www.puzi7.net\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift switch \u8bed\u53e5 - \u8c31\u5b50\u68cb - Puzi7","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.puzi7.net\/?p=520","og_locale":"zh_CN","og_type":"article","og_title":"Swift switch \u8bed\u53e5 - \u8c31\u5b50\u68cb - Puzi7","og_description":"Swift switch \u8bed\u53e5 Swift \u6761\u4ef6\u8bed\u53e5 switch \u8bed\u53e5\u5141\u8bb8\u6d4b\u8bd5\u4e00\u4e2a\u53d8\u91cf\u7b49&hellip;Read More","og_url":"https:\/\/www.puzi7.net\/?p=520","og_site_name":"\u8c31\u5b50\u68cb - Puzi7","article_published_time":"2018-02-23T15:38:57+00:00","author":"\u77f3\u8863","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u77f3\u8863"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.puzi7.net\/?p=520#article","isPartOf":{"@id":"https:\/\/www.puzi7.net\/?p=520"},"author":{"name":"\u77f3\u8863","@id":"https:\/\/www.puzi7.net\/#\/schema\/person\/87dd4941c8ec260c1350b7f41980a190"},"headline":"Swift switch \u8bed\u53e5","datePublished":"2018-02-23T15:38:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.puzi7.net\/?p=520"},"wordCount":33,"commentCount":0,"keywords":["iOS","Swift"],"articleSection":["Mobile","Swift"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.puzi7.net\/?p=520#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.puzi7.net\/?p=520","url":"https:\/\/www.puzi7.net\/?p=520","name":"Swift switch \u8bed\u53e5 - \u8c31\u5b50\u68cb - Puzi7","isPartOf":{"@id":"https:\/\/www.puzi7.net\/#website"},"datePublished":"2018-02-23T15:38:57+00:00","author":{"@id":"https:\/\/www.puzi7.net\/#\/schema\/person\/87dd4941c8ec260c1350b7f41980a190"},"breadcrumb":{"@id":"https:\/\/www.puzi7.net\/?p=520#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.puzi7.net\/?p=520"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.puzi7.net\/?p=520#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.puzi7.net\/"},{"@type":"ListItem","position":2,"name":"\u6280\u672f","item":"https:\/\/www.puzi7.net\/?cat=9"},{"@type":"ListItem","position":3,"name":"Mobile","item":"https:\/\/www.puzi7.net\/?cat=26"},{"@type":"ListItem","position":4,"name":"Swift","item":"https:\/\/www.puzi7.net\/?cat=28"},{"@type":"ListItem","position":5,"name":"Swift switch \u8bed\u53e5"}]},{"@type":"WebSite","@id":"https:\/\/www.puzi7.net\/#website","url":"https:\/\/www.puzi7.net\/","name":"\u8c31\u5b50\u68cb - Puzi7","description":"\u5206\u4eab\uff0c\u6210\u957f","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.puzi7.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.puzi7.net\/#\/schema\/person\/87dd4941c8ec260c1350b7f41980a190","name":"\u77f3\u8863","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/secure.gravatar.com\/avatar\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g","caption":"\u77f3\u8863"},"sameAs":["https:\/\/www.puzi7.net"],"url":"https:\/\/www.puzi7.net\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=520"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/520\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}