{"id":395,"date":"2025-06-02T10:57:46","date_gmt":"2025-06-05T03:26:45","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=395"},"modified":"2025-06-05T10:57:46","modified_gmt":"2025-06-05T03:26:45","slug":"ios-enum-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/ios-enum-%e7%af%84%e4%be%8b\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u7406\u89e3 Enum \u679a\u8209\u7684\u4f7f\u7528\u8207\u61c9\u7528"},"content":{"rendered":"<h1>\u524d\u8a00<\/h1>\n<p>\u5728 Swift \u4e2d\uff0cEnum\uff08\u679a\u8209\uff09\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u8cc7\u6599\u578b\u5225\uff0c\u4e3b\u8981\u529f\u80fd\u662f\u96c6\u4e2d\u7ba1\u7406\u4e00\u7d44\u76f8\u95dc\u7684\u8b8a\u6578\u3002\u4f7f\u7528 Enum \u53ef\u4ee5\u907f\u514d\u5728\u7a0b\u5f0f\u78bc\u4e2d\u56db\u8655\u5c0b\u627e\u9700\u8981\u4fee\u6539\u7684\u8b8a\u6578\uff0c\u4e26\u4e14\u4f7f\u7a0b\u5f0f\u78bc\u66f4\u52a0\u53ef\u8b80\u8207\u7dad\u8b77\u3002\u672c\u6587\u5c07\u4ecb\u7d39 Swift Enum \u7684\u57fa\u672c\u7528\u6cd5\u3001rawValue\u3001\u521d\u59cb\u5316\u51fd\u5f0f\u53ca\u5176\u5ef6\u4f38\u61c9\u7528\uff0c\u8b93\u4f60\u80fd\u5920\u5728\u5be6\u969b\u958b\u767c\u4e2d\u9748\u6d3b\u904b\u7528\u3002<\/p>\n<h2>Swift Enum \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>Enum \u7684\u5ba3\u544a\u8207\u4f7f\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u5982\u4f55\u5ba3\u544a\u4e00\u500b\u57fa\u672c\u7684 Enum\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">enum TestEnum {\n    case money\n    case product\n    case amount\n}\n<\/code><\/pre>\n<p>\u5728\u4f60\u7684 Controller \u4e2d\uff0c\u4f60\u53ef\u4ee5\u9019\u6a23\u547c\u53eb Enum \u7684\u6210\u54e1\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">var money = TestEnum.money\nvar product: TestEnum = .product\n<\/code><\/pre>\n<p>\u9019\u6642\u5019\u4f60\u53ef\u4ee5\u900f\u904e print \u4f86\u6aa2\u67e5\u7d50\u679c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">print(money)  \/\/ \u8f38\u51fa: money\nprint(product) \/\/ \u8f38\u51fa: product\n<\/code><\/pre>\n<h2>\u4f7f\u7528 rawValue \u4f86\u5132\u5b58\u503c<\/h2>\n<p>Enum \u9664\u4e86\u53ef\u4ee5\u5b9a\u7fa9\u6210\u54e1\u5916\uff0c\u9084\u53ef\u4ee5\u7d50\u5408 rawValue \u4f86\u5132\u5b58\u984d\u5916\u7684\u8cc7\u8a0a\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5305\u542b rawValue \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">enum TestEnumWithRawValue: String {\n    case money = \"$250\"\n    case product = \"\u5c0f\u6d77\u87ba\"\n    case amount = \"5\u5143\"\n}\n<\/code><\/pre>\n<p>\u5728 Controller \u4e2d\uff0c\u4f60\u53ef\u4ee5\u9019\u6a23\u547c\u53eb rawValue\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">print(\"\u50f9\u683c:\\(TestEnumWithRawValue.money.rawValue)\")\nprint(\"\u7522\u54c1:\\(TestEnumWithRawValue.product.rawValue)\")\nprint(\"\u6578\u91cf:\\(TestEnumWithRawValue.amount.rawValue)\")\n<\/code><\/pre>\n<p>\u7d50\u679c\u5c07\u6703\u662f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\u50f9\u683c:$250\n\u7522\u54c1:\u5c0f\u6d77\u87ba\n\u6578\u91cf:5\u5143\n<\/code><\/pre>\n<h2>Enum \u7684\u521d\u59cb\u5316\u51fd\u5f0f<\/h2>\n<p>Enum \u4e5f\u53ef\u4ee5\u5b9a\u7fa9\u521d\u59cb\u5316\u51fd\u5f0f\uff0c\u8207 class \u548c struct \u76f8\u4f3c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">enum TestEnumWithInit {\n    case money\n    case product\n    case amount\n\n    init() {\n        print(\"\u521d\u59cb\u5316\u6e2c\u8a66\")\n    }\n}\n<\/code><\/pre>\n<p>\u7576\u4f60\u521d\u59cb\u5316\u9019\u500b Enum \u6642\uff0c\u6703\u81ea\u52d5\u547c\u53eb\u521d\u59cb\u5316\u51fd\u5f0f\uff0c\u4e26\u8f38\u51fa &#8220;\u521d\u59cb\u5316\u6e2c\u8a66&#8221;\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 Enum \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; \u5fd8\u8a18\u4f7f\u7528 Enum \u7684\u6210\u54e1\u6642\u672a\u6b63\u78ba\u6307\u5b9a\u578b\u5225\uff0c\u5c0e\u81f4\u7de8\u8b6f\u932f\u8aa4\u3002<br \/>\n&#8211; \u5617\u8a66\u5c07 rawValue \u8207 Enum \u6210\u54e1\u4e0d\u5339\u914d\u6642\uff0c\u6703\u5c0e\u81f4\u904b\u884c\u6642\u932f\u8aa4\u3002<\/p>\n<p>\u6700\u4f73\u5be6\u8e10\uff1a<br \/>\n&#8211; \u70ba Enum \u6210\u54e1\u4f7f\u7528\u6e05\u6670\u4e14\u5177\u63cf\u8ff0\u6027\u7684\u540d\u7a31\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u3002<br \/>\n&#8211; \u5229\u7528 rawValue \u5132\u5b58\u5fc5\u8981\u7684\u8cc7\u8a0a\uff0c\u6e1b\u5c11\u91cd\u8907\u4ee3\u78bc\u3002<br \/>\n&#8211; \u5728\u9700\u8981\u5b9a\u7fa9\u5927\u91cf\u76f8\u4f3c\u5e38\u6578\u6642\uff0c\u8003\u616e\u4f7f\u7528 Enum \u4f86\u63d0\u9ad8\u7d50\u69cb\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift \u7a0b\u5f0f\u6559\u5b78\u4e2d\u7684 Enum \u679a\u8209\u793a\u610f\u5716\" title=\"\"><\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: Enum \u53ef\u4ee5\u6709\u65b9\u6cd5\u55ce\uff1f<\/h3>\n<p>A1: \u662f\u7684\uff0cEnum \u53ef\u4ee5\u6709\u65b9\u6cd5\uff0c\u9019\u8b93\u4f60\u80fd\u5920\u70ba Enum \u589e\u52a0\u884c\u70ba\uff0c\u589e\u5f37\u5176\u529f\u80fd\u6027\u3002<\/p>\n<h3>Q2: Enum \u7684 rawValue \u53ef\u4ee5\u662f\u4ec0\u9ebc\u578b\u5225\uff1f<\/h3>\n<p>A2: Enum \u7684 rawValue \u53ef\u4ee5\u662f\u6574\u6578\u3001\u5b57\u4e32\u6216\u6d6e\u9ede\u6578\u7b49\u57fa\u672c\u578b\u5225\uff0c\u4f46\u5fc5\u9808\u5728\u5ba3\u544a\u6642\u660e\u78ba\u6307\u5b9a\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u4f7f\u7528 Enum \u9032\u884c\u6a21\u5f0f\u5339\u914d\uff1f<\/h3>\n<p>A3: \u4f60\u53ef\u4ee5\u4f7f\u7528 switch \u8a9e\u53e5\u5c0d Enum \u9032\u884c\u6a21\u5f0f\u5339\u914d\uff0c\u6839\u64da\u4e0d\u540c\u6210\u54e1\u57f7\u884c\u4e0d\u540c\u7684\u4ee3\u78bc\u5206\u652f\uff0c\u9019\u5728\u8655\u7406\u591a\u7a2e\u72c0\u614b\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enum (\u5f15\u310b\u3124\u02cb)<br \/>\n\u4e3b\u8981\u529f\u80fd\u5c31\u662f\u96c6\u4e2d\u4e00\u4e9b\u8b8a\u6578<br \/>\n\u8a2d\u5b9a\u5728Enum<br \/>\n\u5230\u6642\u5019\u5c31\u4e0d\u7528\u518d\u7a0b\u5f0f\u6d77\u4e2d<br \/>\n\u5c0b\u627e\u4f60\u8981\u6539\u7684\u8b8a\u6578<br \/>\n\u800c\u4e14\u4e0d\u7528\u5728\u6539\u5f88\u591a\u500b\u8b8a\u6578<br \/>\n\u60f3\u6539\u5c31\u6539\u4ed6\u5c31\u5920\u4e86<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-395","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-6n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=395"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":696,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/395\/revisions\/696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}