{"id":4253,"date":"2025-06-02T11:52:10","date_gmt":"2025-06-05T04:31:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4253"},"modified":"2025-06-05T11:52:10","modified_gmt":"2025-06-05T04:31:09","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e4%b8%8d%e8%83%bd%e5%b0%8dget-only%e5%b1%ac%e6%80%a7%e9%80%b2%e8%a1%8c%e8%b3%a6%e5%80%bc%e7%9a%84%e5%8e%9f%e5%9b%a0-5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e4%b8%8d%e8%83%bd%e5%b0%8dget-only%e5%b1%ac%e6%80%a7%e9%80%b2%e8%a1%8c%e8%b3%a6%e5%80%bc%e7%9a%84%e5%8e%9f%e5%9b%a0-5\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u7684\u300cCannot assign to value: &#8216;X&#8217; is a get-only property\u300d\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Get-only Property, \u8ce6\u503c\u932f\u8aa4, \u5c6c\u6027\u64cd\u4f5c, Swift \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u7684\u300cCannot assign to value: &#8216;X&#8217; is a get-only property\u300d\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6cd5<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u8a31\u591a\u529f\u80fd\u4ee5\u7c21\u5316\u958b\u767c\u904e\u7a0b\u3002\u7136\u800c\uff0c\u7576\u4f60\u5617\u8a66\u57f7\u884c\u4e0d\u6b63\u78ba\u7684\u64cd\u4f5c\u6642\uff0cSwift \u6703\u63d0\u4f9b\u660e\u78ba\u7684\u932f\u8aa4\u8a0a\u606f\uff0c\u5e6b\u52a9\u4f60\u7406\u89e3\u554f\u984c\u6240\u5728\u3002\u5176\u4e2d\uff0c\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u662f\u300cCannot assign to value: &#8216;X&#8217; is a get-only property\u300d\u3002\u9019\u500b\u932f\u8aa4\u544a\u8a34\u6211\u5011\uff0c\u5617\u8a66\u5c0d\u4e00\u500b\u53ea\u80fd\u8b80\u53d6\u7684\u5c6c\u6027\u9032\u884c\u8ce6\u503c\u662f\u4e0d\u53ef\u884c\u7684\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Get-only \u5c6c\u6027\uff1f<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u53ef\u4ee5\u5c07\u5c6c\u6027\u5b9a\u7fa9\u70ba\u53ea\u80fd\u8b80\u53d6\uff08get-only\uff09\uff0c\u9019\u610f\u5473\u8457\u4f60\u53ef\u4ee5\u53d6\u5f97\u8a72\u5c6c\u6027\u7684\u503c\uff0c\u4f46\u4e0d\u80fd\u9032\u884c\u8ce6\u503c\u3002\u9019\u901a\u5e38\u662f\u7531\u65bc\u5c6c\u6027\u662f\u7528 <code>let<\/code> \u4f86\u5b9a\u7fa9\u7684\uff0c\u8868\u793a\u9019\u500b\u5c6c\u6027\u662f\u5e38\u91cf\u3002<\/p>\n<h2>\u7bc4\u4f8b\u89e3\u6790<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u8aaa\u660e\u9019\u500b\u6982\u5ff5\uff1a<\/p>\n<pre class=\"brush: swift\">\nclass Person {\n    let name: String\n    \n    init(name: String) {\n        self.name = name\n    }\n}\n<\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>name<\/code> \u5c6c\u6027\u662f\u53ea\u80fd\u8b80\u53d6\u7684\uff0c\u56e0\u70ba\u5b83\u88ab\u5b9a\u7fa9\u70ba <code>let<\/code>\uff0c\u800c\u4e0d\u662f <code>var<\/code>\u3002\u9019\u610f\u5473\u8457\u4f60\u53ef\u4ee5\u5f9e <code>name<\/code> \u5c6c\u6027\u4e2d\u53d6\u5f97\u503c\uff0c\u4f46\u7121\u6cd5\u6539\u8b8a\u5b83\uff1a<\/p>\n<pre class=\"brush: swift\">\nlet person = Person(name: \"John\")\n\n\/\/ \u9019\u4e00\u884c\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\nlet name = person.name\n\n\/\/ \u4ee5\u4e0b\u9019\u884c\u6703\u5c0e\u81f4\u932f\u8aa4\n\/\/ person.name = \"Jane\" \/\/ Cannot assign to value: 'name' is a get-only property\n<\/pre>\n<h2>\u5982\u4f55\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff1f<\/h2>\n<p>\u82e5\u4f60\u9700\u8981\u5c0d\u67d0\u500b\u5c6c\u6027\u9032\u884c\u8ce6\u503c\uff0c\u4f60\u61c9\u8a72\u5c07\u5b83\u5b9a\u7fa9\u70ba <code>var<\/code>\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: swift\">\nclass Person {\n    var name: String\n    \n    init(name: String) {\n        self.name = name\n    }\n}\n\nlet person = Person(name: \"John\")\nperson.name = \"Jane\" \/\/ \u73fe\u5728\u9019\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\n<\/pre>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728 Swift \u7684\u5be6\u4f5c\u4e2d\uff0c\u8a2d\u8a08\u5c6c\u6027\u6642\u61c9\u8003\u616e\u53ef\u8b8a\u6027\u3002\u6709\u6642\u5019\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5c6c\u6027\u662f\u53ef\u8b80\u7684\u540c\u6642\u53c8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u908f\u8f2f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>computed properties<\/code> \u4f86\u7372\u5f97\u9019\u7a2e\u529f\u80fd\uff1a<\/p>\n<pre class=\"brush: swift\">\nclass Person {\n    private var _name: String\n    \n    var name: String {\n        return _name\n    }\n    \n    init(name: String) {\n        self._name = name\n    }\n    \n    func updateName(newName: String) {\n        _name = newName\n    }\n}\n\nlet person = Person(name: \"John\")\nperson.updateName(newName: \"Jane\") \/\/ \u66f4\u65b0\u540d\u7a31\nprint(person.name) \/\/ \u8f38\u51fa \"Jane\"\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u4f60\u5728\u7a0b\u5f0f\u4e2d\u9047\u5230\u300cCannot assign to value: &#8216;X&#8217; is a get-only property\u300d\u7684\u932f\u8aa4\uff0c\u9996\u5148\u6aa2\u67e5\u8a72\u5c6c\u6027\u7684\u5b9a\u7fa9\uff0c\u78ba\u8a8d\u5b83\u662f <code>let<\/code> \u9084\u662f <code>var<\/code>\u3002\u82e5\u9700\u8981\u5c0d\u5176\u8ce6\u503c\uff0c\u8a18\u5f97\u5c07\u5c6c\u6027\u6539\u70ba <code>var<\/code>\uff0c\u6216\u901a\u904e\u5176\u4ed6\u65b9\u6cd5\u4f86\u66f4\u65b0\u5176\u503c\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u800c\u8a00\u4e4b\uff0c\u300cCannot assign to value: &#8216;X&#8217; is a get-only property\u300d\u662f\u4e00\u500b\u5e38\u898b\u7684 Swift \u932f\u8aa4\uff0c\u8868\u793a\u5617\u8a66\u5c0d\u53ea\u80fd\u8b80\u53d6\u7684\u5c6c\u6027\u8ce6\u503c\u3002\u4e86\u89e3\u5c6c\u6027\u7684\u5b9a\u7fa9\u53ca\u5176\u884c\u70ba\uff0c\u5c07\u6709\u52a9\u65bc\u4f60\u66f4\u6709\u6548\u5730\u4f7f\u7528 Swift \u958b\u767c\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u77e5\u9053\u6211\u7684\u5c6c\u6027\u662f\u53ef\u4ee5\u8b80\u53d6\u9084\u662f\u53ef\u5beb\u5165\u7684\uff1f<\/h3>\n<p>A1: \u6aa2\u67e5\u5c6c\u6027\u7684\u5b9a\u7fa9\uff0c\u5982\u679c\u4f7f\u7528 <code>let<\/code>\uff0c\u5247\u662f\u53ea\u80fd\u8b80\u53d6\u7684\uff1b\u5982\u679c\u4f7f\u7528 <code>var<\/code>\uff0c\u5247\u662f\u53ef\u5beb\u5165\u7684\u3002<\/p>\n<h3>Q2: \u5728 Swift \u4e2d\u6709\u6c92\u6709\u5176\u4ed6\u985e\u4f3c\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0c\u9084\u6709\u5176\u4ed6\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u7b49\uff0c\u9019\u901a\u5e38\u662f\u56e0\u70ba\u5c0d\u8c61\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u672a\u6b63\u78ba\u5b9a\u7fa9\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u6709\u6548\u7ba1\u7406\u5c6c\u6027\u8b8a\u66f4\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u4f7f\u7528 <code>computed properties<\/code> \u6216\u8005\u65b9\u6cd5\u4f86\u5c01\u88dd\u5c6c\u6027\u7684\u8b8a\u66f4\u908f\u8f2f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u63a7\u5236\u5c6c\u6027\u7684\u8b80\u53d6\u8207\u5beb\u5165\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u89e3\u91cbSwift\u4e2d\u4e0d\u80fd\u5c0dGet-Only\u5c6c\u6027\u9032\u884c\u8ce6\u503c\u7684\u539f\u56e0\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u6b63\u78ba\u4f7f\u7528Get-Only\u5c6c\u6027\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-4253","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-16B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4253","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=4253"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4253\/revisions"}],"predecessor-version":[{"id":4254,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4253\/revisions\/4254"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}