{"id":384,"date":"2025-05-30T10:30:51","date_gmt":"2025-06-05T03:24:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=384"},"modified":"2025-06-05T10:30:51","modified_gmt":"2025-06-05T03:24:50","slug":"swift-struct-%e5%9f%ba%e6%9c%ac%e4%bb%8b%e7%b4%b9%e9%81%8b%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-struct-%e5%9f%ba%e6%9c%ac%e4%bb%8b%e7%b4%b9%e9%81%8b%e7%94%a8\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22 Swift Struct\uff1a2025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<h1>Swift Struct \u57fa\u672c\u4ecb\u7d39\u8207\u904b\u7528<\/h1>\n<p>Swift \u7684 `struct` \u662f\u4e00\u7a2e\u975e\u5e38\u91cd\u8981\u7684\u6578\u64da\u985e\u578b\uff0c\u8207 `class` \u985e\u4f3c\uff0c\u4f46\u6709\u4e00\u4e9b\u95dc\u9375\u7684\u5340\u5225\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39 Swift \u7684 `struct`\uff0c\u4e26\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>Swift Struct \u7684\u57fa\u672c\u7d50\u69cb<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c`struct` \u53ef\u4ee5\u7528\u4f86\u5c01\u88dd\u6578\u64da\u548c\u884c\u70ba\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 `struct` \u7684\u4f8b\u5b50\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nstruct TestStruct {<br \/>\n    let product: String<\/p>\n<p>    init(name: String) {<br \/>\n        self.product = name<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `TestStruct` \u7684\u7d50\u69cb\uff0c\u4e26\u900f\u904e\u521d\u59cb\u5316\u65b9\u6cd5\u8a2d\u7f6e `product` \u5c6c\u6027\u7684\u503c\u3002<\/p>\n<h2>\u4f7f\u7528 Struct \u7684\u65b9\u6cd5<\/h2>\n<p>\u4f7f\u7528 `struct` \u4e5f\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5275\u5efa\u548c\u4f7f\u7528 `TestStruct` \u7684\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet testStruct = TestStruct(name: &#8220;\u5c0f\u6d77\u87ba&#8221;)<br \/>\nprint(testStruct.product) \/\/ \u8f38\u51fa\uff1a\u5c0f\u6d77\u87ba<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u8f38\u51fa `product` \u7684\u503c\u3002<\/p>\n<h2>\u5728 Struct \u4e2d\u6dfb\u52a0\u65b9\u6cd5<\/h2>\n<p>`struct` \u5167\u90e8\u4e0d\u50c5\u53ef\u4ee5\u5305\u542b\u5c6c\u6027\uff0c\u9084\u53ef\u4ee5\u5305\u542b\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u65b9\u6cd5\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nstruct TestStruct {<br \/>\n    let product: String<br \/>\n    let money: Int<\/p>\n<p>    init(name: String, price: Int) {<br \/>\n        self.product = name<br \/>\n        self.money = price<br \/>\n    }<\/p>\n<p>    func showPrice() {<br \/>\n        print(self.money)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u4f60\u53ef\u4ee5\u9019\u6a23\u547c\u53eb `showPrice` \u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet testStruct = TestStruct(name: &#8220;\u5c0f\u6d77\u87ba&#8221;, price: 150)<br \/>\ntestStruct.showPrice() \/\/ \u8f38\u51fa\uff1a150<br \/>\n&#8220;`<\/p>\n<h2>\u81ea\u52d5\u521d\u59cb\u5316\u8207 Enum \u7684\u61c9\u7528<\/h2>\n<p>Swift \u4e2d\u7684 `struct` \u4e5f\u53ef\u4ee5\u81ea\u52d5\u521d\u59cb\u5316\u7f3a\u5931\u7684\u5c6c\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 `enum` \u7684\u4f8b\u5b50\u4f86\u8c50\u5bcc\u7d50\u69cb\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nenum TestEnum {<br \/>\n    case productA<br \/>\n    case productB<br \/>\n    case productC<br \/>\n    case productD<br \/>\n}<\/p>\n<p>struct TestStruct {<br \/>\n    let product: TestEnum<br \/>\n    let money: Int<br \/>\n    let date: String<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u7576\u4f60\u9700\u8981\u5275\u5efa `TestStruct` \u7684\u5be6\u4f8b\u6642\uff0c\u53ef\u4ee5\u9019\u9ebc\u505a\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet testStruct = TestStruct(product: .productA, money: 150, date: &#8220;2025-06-01&#8221;)<br \/>\nprint(testStruct.product) \/\/ \u8f38\u51fa\uff1aproductA<br \/>\nprint(testStruct.money)   \/\/ \u8f38\u51fa\uff1a150<br \/>\nprint(testStruct.date)    \/\/ \u8f38\u51fa\uff1a2025-06-01<br \/>\n&#8220;`<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u7684 `struct` \u63d0\u4f9b\u4e86\u4e00\u7a2e\u5f37\u5927\u800c\u9748\u6d3b\u7684\u65b9\u6cd5\u4f86\u7d44\u7e54\u548c\u7ba1\u7406\u6578\u64da\u3002\u8207 `class` \u76f8\u6bd4\uff0c`struct` \u66f4\u52a0\u8f15\u91cf\uff0c\u4e26\u4e14\u5728\u4e00\u4e9b\u60c5\u5883\u4e0b\u66f4\u70ba\u5b89\u5168\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 Swift \u7684 `struct`\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\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift Struct \u793a\u4f8b\u5716\" title=\"\"><br \/>\n<\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: Swift \u4e2d struct \u548c class \u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>A1: `struct` \u662f\u503c\u985e\u578b\uff0c\u800c `class` \u662f\u5f15\u7528\u985e\u578b\u3002\u9019\u610f\u5473\u8457\u7576\u4f60\u5c07 `struct` \u7684\u5be6\u4f8b\u8ce6\u503c\u7d66\u53e6\u4e00\u500b\u8b8a\u91cf\u6642\uff0c\u6703\u5275\u5efa\u4e00\u500b\u526f\u672c\uff1b\u800c `class` \u5247\u662f\u5c0d\u540c\u4e00\u5be6\u4f8b\u7684\u5f15\u7528\u3002<\/p>\n<h3>Q2: Swift struct \u53ef\u4ee5\u5305\u542b\u54ea\u4e9b\u5167\u5bb9\uff1f<\/h3>\n<p>A2: Swift \u7684 `struct` \u53ef\u4ee5\u5305\u542b\u5c6c\u6027\u3001\u65b9\u6cd5\u3001\u521d\u59cb\u5316\u5668\u3001\u4ee5\u53ca\u9075\u5faa\u5354\u8b70\u7b49\u3002<\/p>\n<h3>Q3: \u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u61c9\u8a72\u4f7f\u7528 struct \u800c\u4e0d\u662f class\uff1f<\/h3>\n<p>A3: \u7576\u4f60\u9700\u8981\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u7d50\u69cb\uff0c\u4e26\u4e14\u4e0d\u9700\u8981\u7e7c\u627f\u529f\u80fd\u6642\uff0c\u4f7f\u7528 `struct` \u662f\u66f4\u597d\u7684\u9078\u64c7\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>struct TestStruct {<br \/>\n    let product: String<br \/>\n    init(name : String) {<br \/>\n        self.product = name<br \/>\n    }<br \/>\n}<\/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-384","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-6c","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/384","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=384"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/384\/revisions"}],"predecessor-version":[{"id":583,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/384\/revisions\/583"}],"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=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}