{"id":4123,"date":"2025-06-01T11:19:36","date_gmt":"2025-06-05T04:12:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4123"},"modified":"2025-06-05T11:19:36","modified_gmt":"2025-06-05T04:12:36","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%advalueoftypexhasnomembery%e9%8c%af%e8%aa%a4%e8%a7%a3%e6%9e%90-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e4%ba%86%e8%a7%a3swift%e4%b8%advalueoftypexhasnomembery%e9%8c%af%e8%aa%a4%e8%a7%a3%e6%9e%90-2\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u89e3\u6c7a\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, \u932f\u8aa4\u89e3\u6790, Value of type 'X' has no member 'Y', Swift \u932f\u8aa4\u8655\u7406, Swift \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u89e3\u6c7a\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5177\u6709\u985e\u578b\u5b89\u5168\u7684\u7279\u6027\u3002\u7136\u800c\uff0c\u5373\u4f7f\u662f\u7d93\u9a57\u8c50\u5bcc\u7684\u958b\u767c\u8005\uff0c\u6709\u6642\u4e5f\u6703\u9047\u5230\u932f\u8aa4\u8a0a\u606f\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u3002\u9019\u500b\u932f\u8aa4\u53ef\u80fd\u6703\u8b93\u4eba\u611f\u5230\u56f0\u60d1\uff0c\u4f46\u901a\u904e\u672c\u7bc7\u6587\u7ae0\uff0c\u60a8\u5c07\u5b78\u6703\u5982\u4f55\u5feb\u901f\u6392\u89e3\u9019\u500b\u554f\u984c\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\uff1f<\/h2>\n<p>\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u662f Swift \u7de8\u8b6f\u5668\u7684\u4e00\u500b\u932f\u8aa4\u8a0a\u606f\uff0c\u8868\u793a\u60a8\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u9019\u901a\u5e38\u767c\u751f\u5728\u4ee5\u4e0b\u5e7e\u7a2e\u60c5\u6cc1\uff1a<\/p>\n<ul>\n<li>\u5617\u8a66\u8a2a\u554f\u932f\u8aa4\u985e\u578b\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002<\/li>\n<li>\u62fc\u5beb\u932f\u8aa4\u6216\u5927\u5c0f\u5beb\u4e0d\u6b63\u78ba\u3002<\/li>\n<li>\u985e\u578b\u672a\u6b63\u78ba\u5c0e\u5165\u6216\u521d\u59cb\u5316\u3002<\/li>\n<\/ul>\n<h2>\u5982\u4f55\u89e3\u6c7a\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\uff1f<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u6c7a\u6b64\u932f\u8aa4\u7684\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<h3>1. \u6aa2\u67e5\u985e\u578b\u8207\u5c6c\u6027<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u8a8d\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u985e\u578b\u662f\u5426\u5be6\u969b\u5b9a\u7fa9\u4e86\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u4f7f\u7528 Xcode \u7684\u81ea\u52d5\u5b8c\u6210\u529f\u80fd\u53ef\u4ee5\u5e6b\u52a9\u60a8\u6aa2\u67e5\u53ef\u7528\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<h3>2. \u6aa2\u67e5\u62fc\u5beb\u8207\u5927\u5c0f\u5beb<\/h3>\n<p>Swift \u662f\u4e00\u7a2e\u5340\u5206\u5927\u5c0f\u5beb\u7684\u8a9e\u8a00\uff0c\u56e0\u6b64\u78ba\u4fdd\u60a8\u6b63\u78ba\u62fc\u5beb\u4e86\u5c6c\u6027\u6216\u65b9\u6cd5\u540d\u7a31\u3002\u8209\u4f8b\u4f86\u8aaa\uff0c<code>myVariable<\/code> \u548c <code>myvariable<\/code> \u662f\u4e0d\u540c\u7684\u8b8a\u6578\u3002<\/p>\n<h3>3. \u78ba\u8a8d\u985e\u578b\u7684\u5c0e\u5165<\/h3>\n<p>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u7b2c\u4e09\u65b9\u5eab\u6216\u6846\u67b6\uff0c\u8acb\u78ba\u8a8d\u60a8\u5df2\u6b63\u78ba\u5c0e\u5165\u8a72\u985e\u578b\uff0c\u4e26\u4e14\u5b83\u5728\u60a8\u7684\u7bc4\u570d\u5167\u53ef\u898b\u3002<\/p>\n<h3>4. \u4f7f\u7528\u53ef\u9078\u93c8\u63a5<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u5c0d\u65bc\u53ef\u9078\u985e\u578b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u53ef\u9078\u93c8\u63a5\u4f86\u5b89\u5168\u5730\u8a2a\u554f\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>let result = myOptionalVariable?.myMethod()<\/code><\/pre>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6b63\u78ba\u4f7f\u7528\u985e\u578b\u548c\u5176\u5c6c\u6027\uff1a<\/p>\n<pre><code>class Person {\n    var name: String\n    init(name: String) {\n        self.name = name\n    }\n}\n\nlet person = Person(name: \"Alice\")\n\/\/ \u6b63\u78ba\u8a2a\u554f\u5c6c\u6027\nprint(person.name)  \/\/ \u8f38\u51fa: Alice\n\/\/ \u932f\u8aa4\u7bc4\u4f8b\uff1a\u5c07\u6703\u5c0e\u81f4\u300cValue of type 'Person' has no member 'age'\u300d\n\/\/ print(person.age)\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u60a8\u4ecd\u7136\u9047\u5230\u554f\u984c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u9805\u76ee\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u985e\u578b\u5df2\u6b63\u78ba\u521d\u59cb\u5316\u3002<\/li>\n<li>\u6aa2\u67e5\u7bc4\u570d\u5167\u662f\u5426\u5b58\u5728\u547d\u540d\u885d\u7a81\u3002<\/li>\n<li>\u4f7f\u7528 Swift \u7684\u8abf\u8a66\u5de5\u5177\u4f86\u9010\u6b65\u6aa2\u67e5\u8b8a\u6578\u7684\u72c0\u614b\u3002<\/li>\n<\/ul>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u932f\u8aa4\u662f Swift \u7de8\u8b6f\u5668\u767c\u51fa\u7684\u8b66\u544a\uff0c\u9019\u8868\u793a\u60a8\u5617\u8a66\u4f7f\u7528\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u901a\u904e\u6aa2\u67e5\u985e\u578b\u3001\u62fc\u5beb\u3001\u5c0e\u5165\u8207\u7bc4\u570d\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u6b64\u554f\u984c\uff0c\u4e26\u63d0\u5347\u60a8\u7684 Swift \u7de8\u7a0b\u6280\u5de7\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\u985e\u578b\u662f\u5426\u5305\u542b\u7279\u5b9a\u5c6c\u6027\u6216\u65b9\u6cd5\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528 Xcode \u7684\u81ea\u52d5\u5b8c\u6210\u529f\u80fd\uff0c\u4e26\u67e5\u770b\u8a72\u985e\u578b\u7684\u5b9a\u7fa9\u548c\u53ef\u7528\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u5217\u8868\u3002<\/p>\n<h3>Q2: \u5982\u679c\u6211\u9047\u5230\u5176\u4ed6 Swift \u932f\u8aa4\uff0c\u6211\u8a72\u5982\u4f55\u8655\u7406\uff1f<\/h3>\n<p>A2: \u5efa\u8b70\u67e5\u95b1\u5b98\u65b9 Swift \u6587\u6a94\uff0c\u6216\u5728\u958b\u767c\u8005\u793e\u5340\u4e0a\u5c0b\u6c42\u5e6b\u52a9\uff0c\u901a\u5e38\u6703\u6709\u89e3\u6c7a\u65b9\u6848\u6216\u5efa\u8b70\u3002<\/p>\n<h3>Q3: \u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e6b\u52a9\u6211\u6392\u9664 Swift \u932f\u8aa4\uff1f<\/h3>\n<p>A3: \u4f7f\u7528 Xcode \u7684\u8abf\u8a66\u5de5\u5177\u3001SwiftLint \u9032\u884c\u4ee3\u78bc\u6aa2\u67e5\uff0c\u4ee5\u53ca\u5728\u958b\u767c\u8005\u8ad6\u58c7\u4e0a\u5c0b\u6c42\u5e6b\u52a9\u90fd\u662f\u4e0d\u932f\u7684\u9078\u64c7\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Swift\u4e2dValueofType&#8217;X&#8217;HasNoMember&#8217;Y&#8217;\u932f\u8aa4\u89e3\u6790\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u52a0\u4e86\u89e3Swift\u4e2d\u7684\u932f\u8aa4\u89e3\u6790\uff0c\u4e26\u4e14\u66f4\u52a0\u719f\u6089Swift\u7684\u8a9e\u6cd5\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-4123","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-14v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4123","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=4123"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4123\/revisions"}],"predecessor-version":[{"id":4124,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4123\/revisions\/4124"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}