{"id":4087,"date":"2025-05-29T11:10:13","date_gmt":"2025-06-05T04:08:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4087"},"modified":"2025-06-05T11:10:13","modified_gmt":"2025-06-05T04:08:13","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e4%b8%8d%e5%8f%af%e8%ae%8a%e5%80%bc%e7%9a%84%e7%aa%81%e8%ae%8a%e5%8f%96%e5%80%bc","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e4%b8%8d%e5%8f%af%e8%ae%8a%e5%80%bc%e7%9a%84%e7%aa%81%e8%ae%8a%e5%8f%96%e5%80%bc\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u4e2d\u4e0d\u53ef\u8b8a\u503c\u7684\u7a81\u8b8a\u53d6\u503c\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Mutating Getter, Immutable Value, Swift \u6559\u5b78, Swift \u932f\u8aa4\u6392\u9664\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u4e2d\u4e0d\u53ef\u8b8a\u503c\u7684\u7a81\u8b8a\u53d6\u503c\u932f\u8aa4<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u9069\u7528\u65bc iOS \u548c macOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u3002\u5728\u4f7f\u7528 Swift \u6642\uff0c\u6211\u5011\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5176\u4e2d\u4e4b\u4e00\u662f\u300cCannot use mutating getter on immutable value\u300d\u3002\u9019\u500b\u932f\u8aa4\u8868\u793a\u4f60\u5617\u8a66\u5728\u4e00\u500b\u4e0d\u53ef\u8b8a\u7684\u503c\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u503c\u5668\u3002<\/p>\n<h2>\u932f\u8aa4\u539f\u56e0\u89e3\u6790<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u8b8a\u6578\u53ef\u4ee5\u88ab\u5b9a\u7fa9\u70ba\u53ef\u8b8a\uff08`var`\uff09\u6216\u4e0d\u53ef\u8b8a\uff08`let`\uff09\u3002\u53ef\u8b8a\u7684\u8b8a\u6578\u53ef\u4ee5\u88ab\u6539\u8b8a\uff0c\u800c\u4e0d\u53ef\u8b8a\u7684\u8b8a\u6578\u5247\u7121\u6cd5\u88ab\u6539\u8b8a\u3002\u5982\u679c\u4f60\u5617\u8a66\u5728\u4e0d\u53ef\u8b8a\u7684\u8b8a\u6578\u4e0a\u4f7f\u7528\u8b8a\u66f4\u64cd\u4f5c\uff0c\u5c31\u6703\u5c0e\u81f4\u9019\u500b\u932f\u8aa4\u3002\u8209\u4f8b\u4f86\u8aaa\uff0c\u7576\u4f60\u5617\u8a66\u4f7f\u7528 mutating \u65b9\u6cd5\u4f86\u6539\u8b8a\u4e00\u500b\u4e0d\u53ef\u8b8a\u7684\u7269\u4ef6\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u9019\u500b\u932f\u8aa4\u3002<\/p>\n<h2>\u89e3\u6c7a\u65b9\u6cd5<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u8b8a\u6578\u662f\u53ef\u8b8a\u7684\u3002\u4ee5\u4e0b\u662f\u89e3\u6c7a\u6b64\u932f\u8aa4\u7684\u5e7e\u500b\u6b65\u9a5f\u548c\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<p>1. **\u5c07\u8b8a\u6578\u5b9a\u7fa9\u70ba\u53ef\u8b8a\u7684**\uff1a\u4f7f\u7528 `var` \u5b9a\u7fa9\u8b8a\u6578\uff0c\u800c\u4e0d\u662f `let`\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   var myVariable = &#8220;Hello World&#8221;<br \/>\n   myVariable = &#8220;Goodbye World&#8221;<br \/>\n   &#8220;`<\/p>\n<p>2. **\u6aa2\u67e5\u7269\u4ef6\u7684 mutating \u65b9\u6cd5**\uff1a\u5982\u679c\u4f60\u4f7f\u7528\u7684\u65b9\u6cd5\u662f mutating \u7684\uff0c\u78ba\u4fdd\u4f60\u6b63\u5728\u5c0d\u53ef\u8b8a\u7684\u7269\u4ef6\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   struct MyStruct {<br \/>\n       var value: Int<\/p>\n<p>       mutating func updateValue(newValue: Int) {<br \/>\n           self.value = newValue<br \/>\n       }<br \/>\n   }<\/p>\n<p>   var myStruct = MyStruct(value: 10)<br \/>\n   myStruct.updateValue(newValue: 20) \/\/ \u6b63\u78ba\u7684\u7528\u6cd5<br \/>\n   &#8220;`<\/p>\n<p>3. **\u4f7f\u7528 `@State` \u6216 `@Binding`\uff08\u5728 SwiftUI \u4e2d\uff09**\uff1a\u5728 SwiftUI \u4e2d\uff0c\u7576\u4f60\u4f7f\u7528\u72c0\u614b\u6216\u7d81\u5b9a\u6642\uff0c\u61c9\u78ba\u4fdd\u5b83\u5011\u662f\u53ef\u8b8a\u7684\u3002<\/p>\n<p>   &#8220;`swift<br \/>\n   @State private var counter: Int = 0<\/p>\n<p>   var body: some View {<br \/>\n       Button(&#8220;Increment&#8221;) {<br \/>\n           counter += 1 \/\/ \u6b63\u78ba\u7684\u64cd\u4f5c<br \/>\n       }<br \/>\n   }<br \/>\n   &#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u4f60\u4ecd\u7136\u9047\u5230\u6b64\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>&#8211; \u78ba\u4fdd\u6240\u6709\u4f7f\u7528 mutating \u65b9\u6cd5\u7684\u7269\u4ef6\u90fd\u662f\u7528 `var` \u5b9a\u7fa9\u7684\u3002<br \/>\n&#8211; \u6aa2\u67e5\u65b9\u6cd5\u662f\u5426\u6b63\u78ba\u6a19\u8a18\u70ba `mutating`\u3002<br \/>\n&#8211; \u5982\u679c\u4f7f\u7528 SwiftUI\uff0c\u78ba\u8a8d\u6240\u6709\u72c0\u614b\u8b8a\u91cf\u90fd\u662f\u7528 `@State` \u6216 `@Binding` \u5b9a\u7fa9\u7684\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9019\u500b\u6982\u5ff5\u4e0d\u50c5\u9650\u65bc\u57fa\u672c\u7684\u8b8a\u6578\uff0c\u9084\u9069\u7528\u65bc Swift \u4e2d\u7684\u8a31\u591a\u8cc7\u6599\u7d50\u69cb\uff0c\u5982\u9663\u5217\u548c\u5b57\u5178\u3002\u5728\u9032\u884c\u8cc7\u6599\u4fee\u6539\u6642\uff0c\u8acb\u52d9\u5fc5\u6aa2\u67e5\u8cc7\u6599\u7684\u8b8a\u66f4\u6027\uff0c\u4ee5\u907f\u514d\u6b64\u985e\u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u300cCannot use mutating getter on immutable value\u300d\u662f\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u9019\u610f\u5473\u8457\u4f60\u4e0d\u80fd\u5728\u4e0d\u53ef\u8b8a\u7684\u503c\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u503c\u5668\u3002\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u8acb\u78ba\u4fdd\u4f7f\u7528\u53ef\u8b8a\u7684\u8b8a\u6578\u4f86\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u70ba\u4ec0\u9ebc\u6703\u9047\u5230\u300cCannot use mutating getter on immutable value\u300d\u9019\u500b\u932f\u8aa4\uff1f<\/h3>\n<p>A1: \u9019\u662f\u56e0\u70ba\u4f60\u5617\u8a66\u5728\u4e0d\u53ef\u8b8a\u7684\u8b8a\u6578\u4e0a\u4f7f\u7528 mutating \u65b9\u6cd5\uff0c\u89e3\u6c7a\u65b9\u6cd5\u662f\u5c07\u8b8a\u6578\u5b9a\u7fa9\u70ba\u53ef\u8b8a\u7684\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u78ba\u8a8d\u6211\u7684\u8b8a\u6578\u662f\u53ef\u8b8a\u7684\uff1f<\/h3>\n<p>A2: \u6aa2\u67e5\u8b8a\u6578\u7684\u5b9a\u7fa9\uff0c\u78ba\u4fdd\u4f7f\u7528 `var` \u800c\u4e0d\u662f `let`\uff0c\u4e26\u78ba\u8a8d\u8cc7\u6599\u7d50\u69cb\u4e2d\u7684\u65b9\u6cd5\u6a19\u8a18\u70ba `mutating`\u3002<\/p>\n<h3>Q3: SwiftUI \u4e2d\u7684\u72c0\u614b\u61c9\u5982\u4f55\u8655\u7406\u4ee5\u907f\u514d\u6b64\u932f\u8aa4\uff1f<\/h3>\n<p>A3: \u5728 SwiftUI \u4e2d\uff0c\u61c9\u4f7f\u7528 `@State` \u6216 `@Binding` \u4f86\u5b9a\u7fa9\u53ef\u8b8a\u72c0\u614b\uff0c\u5f9e\u800c\u907f\u514d\u4e0d\u53ef\u8b8a\u7684\u932f\u8aa4\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u89e3\u91cbSwift\u4e2d\u4e0d\u53ef\u8b8a\u503c\u7684\u7a81\u8b8a\u53d6\u503c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6280\u5de7\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u5728\u4f7f\u7528Swift\u6642\u907f\u514d\u51fa\u73fe\u932f\u8aa4\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-4087","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-13V","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4087","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=4087"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4087\/revisions"}],"predecessor-version":[{"id":4088,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4087\/revisions\/4088"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}