{"id":4209,"date":"2025-05-31T11:31:23","date_gmt":"2025-06-05T04:23:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4209"},"modified":"2025-06-05T11:31:23","modified_gmt":"2025-06-05T04:23:22","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-4","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-4\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u5982\u4f55\u89e3\u6c7a\u300cCannot use mutating getter on immutable value: &#8216;X&#8217;\u300d"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Error Parsing, Mutating Getter, Immutable Value, Swift Programming, Swift Error Handling\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u5982\u4f55\u89e3\u6c7a\u300cCannot use mutating getter on immutable value: &#8216;X&#8217;\u300d<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc iOS \u548c macOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u3002\u7136\u800c\uff0c\u958b\u767c\u8005\u5728\u4f7f\u7528 Swift \u6642\uff0c\u7d93\u5e38\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\uff0c\u5176\u4e2d\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u662f\u300cCannot use mutating getter on immutable value: &#8216;X&#8217;\u300d\u3002\u9019\u500b\u932f\u8aa4\u7684\u542b\u7fa9\u662f\u4f60\u4e0d\u80fd\u5728\u4e0d\u53ef\u8b8a\u7684\u503c\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\u3002\u672c\u6587\u5c07\u8a73\u7d30\u89e3\u91cb\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u53ca\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u4e0d\u53ef\u8b8a\u7684\u503c\uff1f<\/h2>\n<p>\u4e0d\u53ef\u8b8a\u7684\u503c\u662f\u6307\u4e0d\u80fd\u88ab\u66f4\u6539\u7684\u503c\u3002\u5728 Swift \u4e2d\uff0c\u4e0d\u53ef\u8b8a\u7684\u503c\u662f\u4f7f\u7528 `let` \u95dc\u9375\u5b57\u5ba3\u544a\u7684\u8b8a\u6578\u3002\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u4e0d\u53ef\u8b8a\u7684\u5b57\u4e32\u8b8a\u6578\uff1a<\/p>\n<pre class=\"brush: swift\">let myString = \"Hello World\"<\/pre>\n<p>\u9019\u500b\u8b8a\u6578\u662f\u4e0d\u53ef\u8b8a\u7684\uff0c\u56e0\u70ba\u5b83\u662f\u4f7f\u7528 `let` \u95dc\u9375\u5b57\u5ba3\u544a\u7684\u3002\u7576\u4f60\u5617\u8a66\u4fee\u6539\u9019\u500b\u8b8a\u6578\u7684\u503c\u6642\uff0c\u6703\u51fa\u73fe\u932f\u8aa4\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\uff1f<\/h2>\n<p>\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\u662f\u6307\u53ef\u4ee5\u66f4\u6539\u8b8a\u6578\u503c\u7684\u51fd\u6578\u3002\u5728 Swift \u4e2d\uff0c\u9019\u901a\u5e38\u662f\u6307\u88ab\u6a19\u8a18\u70ba `mutating` \u7684\u65b9\u6cd5\u3002\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u7d50\u69cb\u9ad4\uff0c\u4e26\u5e0c\u671b\u5728\u5176\u5167\u90e8\u4fee\u6539\u67d0\u500b\u5c6c\u6027\uff1a<\/p>\n<pre class=\"brush: swift\">struct Greeting {\n    var message: String\n    \n    mutating func changeMessage(newMessage: String) {\n        message = newMessage\n    }\n}<\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`changeMessage` \u662f\u4e00\u500b\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u66f4\u6539 `message` \u5c6c\u6027\u7684\u503c\u3002<\/p>\n<h2>\u70ba\u4ec0\u9ebc\u4e0d\u80fd\u5728\u4e0d\u53ef\u8b8a\u7684\u503c\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\uff1f<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u5b89\u5168\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u65e8\u5728\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\u3002\u5982\u679c\u4f60\u5617\u8a66\u5728\u4e0d\u53ef\u8b8a\u7684\u503c\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\uff0cSwift \u5c07\u6703\u62cb\u51fa\u4e00\u500b\u932f\u8aa4\uff1a\u300cCannot use mutating getter on immutable value: &#8216;X&#8217;\u300d\u3002\u9019\u662f\u56e0\u70ba\u7de8\u8b6f\u5668\u7121\u6cd5\u78ba\u5b9a\u8a72\u503c\u662f\u5426\u53ef\u4ee5\u66f4\u6539\uff0c\u5f9e\u800c\u4fdd\u8b77\u4f60\u7684\u7a0b\u5f0f\u78bc\u4e0d\u6703\u51fa\u73fe\u610f\u5916\u884c\u70ba\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff1f<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u4f60\u9700\u8981\u5c07\u4e0d\u53ef\u8b8a\u7684\u503c\u66f4\u6539\u70ba\u53ef\u8b8a\u7684\u503c\u3002\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u4e0d\u53ef\u8b8a\u7684\u5b57\u4e32\u8b8a\u6578\uff1a<\/p>\n<pre class=\"brush: swift\">let myString = \"Hello World\"<\/pre>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u8a72\u8b8a\u6578\u66f4\u6539\u70ba\u53ef\u8b8a\u7684\uff1a<\/p>\n<pre class=\"brush: swift\">var myString = \"Hello World\"<\/pre>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u8a72\u8b8a\u6578\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\uff0c\u800c\u4e0d\u6703\u62cb\u51fa\u932f\u8aa4\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684 Swift \u7a0b\u5f0f\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6b63\u78ba\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\uff1a<\/p>\n<pre class=\"brush: swift\">struct User {\n    var name: String\n    \n    mutating func updateName(newName: String) {\n        name = newName\n    }\n}\n\nvar user = User(name: \"Alice\")\nuser.updateName(newName: \"Bob\") \/\/ \u6b63\u78ba\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\nprint(user.name) \/\/ \u8f38\u51fa: Bob\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `mutating` \u65b9\u6cd5\u4f86\u4fee\u6539 `User` \u7d50\u69cb\u9ad4\u7684 `name` \u5c6c\u6027\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u907f\u514d\u51fa\u73fe\u4e0d\u53ef\u8b8a\u503c\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u63d0\u793a<\/h2>\n<p>\u5982\u679c\u4f60\u4ecd\u7136\u9047\u5230\u6b64\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u500b\u8981\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd\u4f60\u5617\u8a66\u4fee\u6539\u7684\u8b8a\u6578\u662f\u4f7f\u7528 `var` \u5ba3\u544a\uff0c\u800c\u975e `let`\u3002<br \/>\n&#8211; \u6aa2\u67e5\u4f60\u662f\u5426\u5728\u7d50\u69cb\u9ad4\u7684\u65b9\u6cd5\u4e2d\u6a19\u8a18\u4e86 `mutating`\uff0c\u4ee5\u5141\u8a31\u4fee\u6539\u5c6c\u6027\u3002<br \/>\n&#8211; \u78ba\u8a8d\u4f60\u7684\u8b8a\u6578\u6c92\u6709\u88ab\u5176\u4ed6\u4e0d\u53ef\u8b8a\u7684\u7bc4\u7587\u6240\u9650\u5236\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728 Swift \u7684\u958b\u767c\u4e2d\uff0c\u4e86\u89e3\u5982\u4f55\u8655\u7406\u4e0d\u53ef\u8b8a\u8207\u53ef\u8b8a\u7684\u503c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9019\u4e0d\u50c5\u50c5\u9069\u7528\u65bc\u57fa\u672c\u7684\u8cc7\u6599\u985e\u578b\uff0c\u9084\u6d89\u53ca\u5230\u81ea\u5b9a\u7fa9\u7d50\u69cb\u548c\u985e\u5225\u7684\u8a2d\u8a08\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u66f4\u5b89\u5168\u548c\u53ef\u7dad\u8b77\u7684\u7a0b\u5f0f\u78bc\uff0c\u63d0\u9ad8\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a73\u7d30\u89e3\u91cb\u4e86 Swift \u932f\u8aa4\u300cCannot use mutating getter on immutable value: &#8216;X&#8217;\u300d\u7684\u542b\u7fa9\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u5b83\u3002\u6211\u5011\u767c\u73fe\uff0c\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u5fc5\u9808\u5c07\u4e0d\u53ef\u8b8a\u7684\u503c\u66f4\u6539\u70ba\u53ef\u8b8a\u7684\u503c\uff0c\u7136\u5f8c\u624d\u80fd\u5728\u8a72\u503c\u4e0a\u4f7f\u7528\u8b8a\u66f4\u7684\u53d6\u5f97\u5668\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u70ba\u4ec0\u9ebc Swift \u8981\u5340\u5206\u53ef\u8b8a\u548c\u4e0d\u53ef\u8b8a\u7684\u503c\uff1f<\/h3>\n<p>Swift \u7684\u8a2d\u8a08\u65e8\u5728\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u5b89\u5168\u6027\u548c\u53ef\u9810\u6e2c\u6027\uff0c\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\u8207\u5d29\u6f70\u3002<\/p>\n<h3>2. \u5982\u4f55\u5224\u65b7\u4e00\u500b\u8b8a\u6578\u662f\u5426\u53ef\u8b8a\uff1f<\/h3>\n<p>\u5982\u679c\u8b8a\u6578\u662f\u4f7f\u7528 `var` \u5ba3\u544a\u7684\uff0c\u90a3\u9ebc\u5b83\u662f\u53ef\u8b8a\u7684\uff1b\u5982\u679c\u662f\u4f7f\u7528 `let` \u5ba3\u544a\u7684\uff0c\u90a3\u9ebc\u5b83\u662f\u4e0d\u53ef\u8b8a\u7684\u3002<\/p>\n<h3>3. \u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u6211\u61c9\u8a72\u4f7f\u7528\u53ef\u8b8a\u503c\uff1f<\/h3>\n<p>\u7576\u4f60\u9700\u8981\u5c0d\u8b8a\u6578\u9032\u884c\u4fee\u6539\u6216\u66f4\u65b0\u6642\uff0c\u61c9\u8a72\u4f7f\u7528\u53ef\u8b8a\u503c\u3002\u9019\u901a\u5e38\u51fa\u73fe\u5728\u9700\u8981\u9032\u884c\u591a\u6b21\u64cd\u4f5c\u7684\u60c5\u6cc1\u4e0b\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\u907f\u514d\u51fa\u73fe\u201cCannotusemutatinggetteronimmutablevalue:&#8217;X&#8217;\u201d\u7684\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-4209","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-15T","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4209","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=4209"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4209\/revisions"}],"predecessor-version":[{"id":4210,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4209\/revisions\/4210"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}