{"id":4077,"date":"2025-05-30T11:27:40","date_gmt":"2025-06-05T04:06:39","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4077"},"modified":"2025-06-05T11:27:40","modified_gmt":"2025-06-05T04:06:39","slug":"%e8%a7%a3%e6%9e%90swift%e9%8c%af%e8%aa%a4%ef%bc%9a%e3%80%8ctypexdoesnothaveamembery%e3%80%8d","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e8%a7%a3%e6%9e%90swift%e9%8c%af%e8%aa%a4%ef%bc%9a%e3%80%8ctypexdoesnothaveamembery%e3%80%8d\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u5982\u4f55\u89e3\u6c7a\u300cType &#8216;X&#8217; does not have a member &#8216;Y&#8217;\u300d"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Error Parsing, Type 'X' does not have a member 'Y'\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u5982\u4f55\u89e3\u6c7a\u300cType &#8216;X&#8217; does not have a member &#8216;Y&#8217;\u300d<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u529f\u80fd\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f46\u7576\u6211\u5011\u7de8\u5beb\u7a0b\u5f0f\u6642\uff0c\u6642\u5e38\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u91cd\u9ede\u63a2\u8a0e\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u8a0a\u606f\uff1a\u300cType &#8216;X&#8217; does not have a member &#8216;Y&#8217;\u300d\u3002\u9019\u500b\u932f\u8aa4\u901a\u5e38\u8868\u793a\u4f60\u6b63\u5728\u5617\u8a66\u4f7f\u7528\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\uff0c\u8b93\u6211\u5011\u4f86\u770b\u770b\u8a72\u5982\u4f55\u6709\u6548\u89e3\u6c7a\u9019\u500b\u554f\u984c\u3002<\/p>\n<h2>\u932f\u8aa4\u89e3\u6790<\/h2>\n<p>\u9019\u7a2e\u932f\u8aa4\u7684\u6839\u672c\u539f\u56e0\u53ef\u4ee5\u6b78\u7d50\u70ba\u5169\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u5c6c\u6027\u6216\u65b9\u6cd5\u4e0d\u5b58\u5728\uff1a<\/strong>\u4f60\u53ef\u80fd\u6b63\u5728\u4f7f\u7528\u4e00\u500b\u985e\u578b\u4e2d\u4e26\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u985e\u578b\u672c\u8eab\u4e0d\u5b58\u5728\uff1a<\/strong>\u4f60\u53ef\u80fd\u5617\u8a66\u4f7f\u7528\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u985e\u578b\u3002<\/li>\n<\/ul>\n<h2>\u5e38\u898b\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5be6\u969b\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u9019\u500b\u932f\u8aa4\u3002<\/p>\n<pre class=\"brush: swift\">\n\/\/ \u932f\u8aa4\u793a\u4f8b 1\nlet myString: String = \"Hello, World!\"\nprint(myString.length) \/\/ Type 'String' does not have a member 'length'\n\n\n\/\/ \u932f\u8aa4\u793a\u4f8b 2\nstruct Foo {}\nlet myFoo = Foo()\nprint(myFoo.bar) \/\/ Type 'Foo' does not have a member 'bar'\n<\/pre>\n<h2>\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u4e9b\u932f\u8aa4\uff0c\u8acb\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u6aa2\u67e5\u4f60\u6240\u4f7f\u7528\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u662f\u5426\u6b63\u78ba\u3002\u8acb\u53c3\u8003\u5b98\u65b9\u6587\u6a94\u4ee5\u78ba\u4fdd\u4f60\u4f7f\u7528\u7684 API \u662f\u6b63\u78ba\u7684\u3002<\/li>\n<li>\u78ba\u8a8d\u985e\u578b\u662f\u5426\u5df2\u5b9a\u7fa9\u3002\u82e5\u4f7f\u7528\u7684\u985e\u578b\u672a\u88ab\u6b63\u78ba\u5b9a\u7fa9\uff0c\u8acb\u6aa2\u67e5\u5176\u547d\u540d\u548c\u7bc4\u7587\u3002<\/li>\n<li>\u5982\u679c\u4f60\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab\uff0c\u8acb\u78ba\u4fdd\u8a72\u5eab\u5df2\u6b63\u78ba\u5c0e\u5165\uff0c\u4e26\u4e14\u4f60\u4f7f\u7528\u7684\u7248\u672c\u652f\u6301\u8a72\u5c6c\u6027\u6216\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u4e0a\u9762\u7684\u5e38\u898b\u60c5\u6cc1\u5916\uff0c\u9019\u7a2e\u932f\u8aa4\u9084\u53ef\u4ee5\u5728\u4ee5\u4e0b\u60c5\u5883\u4e2d\u767c\u751f\uff1a<\/p>\n<ul>\n<li>\u7576\u4f60\u4f7f\u7528 Swift \u7684\u64f4\u5c55\uff08Extension\uff09\u4f86\u6dfb\u52a0\u5c6c\u6027\u6216\u65b9\u6cd5\uff0c\u4f46\u5fd8\u8a18\u5c0e\u5165\u76f8\u61c9\u7684\u6a21\u7d44\u3002<\/li>\n<li>\u5728\u4f7f\u7528\u52d5\u614b\u751f\u6210\u985e\u578b\u6216\u53cd\u5c04\uff08Reflection\uff09\u6642\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u985e\u578b\u6aa2\u67e5\u5931\u6557\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u96d6\u7136\u662f\u4e00\u500b\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f46\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\u4ecd\u9700\u7559\u610f\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5206\u6790\u4e86\u300cType &#8216;X&#8217; does not have a member &#8216;Y&#8217;\u300d\u7684\u5178\u578b\u539f\u56e0\u8207\u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u63d0\u4f9b\u4e86\u5be6\u969b\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\u4f86\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u8a72\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u5f0f\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u77e5\u9053\u4e00\u500b\u5c6c\u6027\u6216\u65b9\u6cd5\u662f\u5426\u5b58\u5728\u65bc\u67d0\u500b\u985e\u578b\u4e2d\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u67e5\u95b1 Swift \u7684\u5b98\u65b9\u6587\u6a94\uff0c\u6216\u8005\u4f7f\u7528 Xcode \u7684\u81ea\u52d5\u88dc\u5168\u529f\u80fd\u4f86\u78ba\u8a8d\u5c6c\u6027\u6216\u65b9\u6cd5\u7684\u5b58\u5728\u6027\u3002<\/p>\n<h3>2. \u5982\u679c\u6211\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab\u6642\u9047\u5230\u6b64\u932f\u8aa4\uff0c\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>\u8acb\u6aa2\u67e5\u4f60\u7684\u5eab\u7248\u672c\uff0c\u4e26\u78ba\u4fdd\u4f60\u4f7f\u7528\u7684 API \u5728\u8a72\u7248\u672c\u4e2d\u662f\u53ef\u7528\u7684\uff0c\u5fc5\u8981\u6642\u67e5\u770b\u5eab\u7684\u5b98\u65b9\u6587\u6a94\u3002<\/p>\n<h3>3. \u6709\u54ea\u4e9b\u5e38\u898b\u7684\u7de8\u7a0b\u7fd2\u6163\u53ef\u4ee5\u907f\u514d\u9019\u7a2e\u932f\u8aa4\uff1f<\/h3>\n<p>\u4fdd\u6301\u6e05\u6670\u7684\u547d\u540d\u898f\u5247\u3001\u4f7f\u7528\u9069\u7576\u7684\u985e\u578b\u6aa2\u67e5\u3001\u4ee5\u53ca\u7d93\u5e38\u53c3\u8003\u5b98\u65b9\u6587\u6a94\u90fd\u662f\u826f\u597d\u7684\u7de8\u7a0b\u7fd2\u6163\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u6b64\u985e\u932f\u8aa4\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Swift\u8a9e\u8a00\u4e2d\u7684\u932f\u8aa4\u8a0a\u606f\u300cType&#8217;X&#8217;doesnothaveamember&#8217;Y&#8217;\u300d\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u89e3\u6c7a\u554f\u984c\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-4077","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-13L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4077","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=4077"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4077\/revisions"}],"predecessor-version":[{"id":4078,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4077\/revisions\/4078"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}