{"id":4205,"date":"2025-05-29T11:51:31","date_gmt":"2025-06-05T04:22:30","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4205"},"modified":"2025-06-05T11:51:31","modified_gmt":"2025-06-05T04:22:30","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%advalueoftypexhasnomembery%e9%8c%af%e8%aa%a4%e8%a7%a3%e6%9e%90-4","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-4\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u5982\u4f55\u89e3\u6c7a\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, \u932f\u8aa4\u89e3\u6790, Value of type 'X' has no member 'Y', Swift \u6559\u5b78, Swift \u7de8\u7a0b\u6280\u5de7\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u5982\u4f55\u89e3\u6c7a\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc iOS \u548c macOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u3002\u4e0d\u904e\uff0c\u5728\u7de8\u5beb\u4ee3\u78bc\u6642\uff0c\u958b\u767c\u8005\u7d93\u5e38\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\u3002\u5176\u4e2d\uff0c\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u662f\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u521d\u5b78\u8005\u53ef\u80fd\u6703\u611f\u5230\u56f0\u60d1\u3002\u4e0d\u904e\uff0c\u9019\u500b\u554f\u984c\u5176\u5be6\u76f8\u5c0d\u5bb9\u6613\u89e3\u6c7a\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u5c07\u8a73\u7d30\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u539f\u56e0\u53ca\u89e3\u6c7a\u65b9\u6848\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\u767c\u51fa\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u8868\u793a\u4f60\u6b63\u5728\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u9019\u610f\u5473\u8457\u5728\u578b\u5225 X \u4e2d\uff0c\u4f60\u6240\u5617\u8a66\u8a2a\u554f\u7684\u5c6c\u6027\u6216\u65b9\u6cd5 Y \u662f\u4e0d\u5b58\u5728\u7684\u3002\u9019\u7a2e\u932f\u8aa4\u901a\u5e38\u767c\u751f\u5728\u4ee5\u4e0b\u5e7e\u7a2e\u60c5\u6cc1\uff1a<\/p>\n<ul>\n<li>\u62fc\u5beb\u932f\u8aa4\uff1a\u4f60\u53ef\u80fd\u62fc\u932f\u4e86\u5c6c\u6027\u6216\u65b9\u6cd5\u7684\u540d\u7a31\u3002<\/li>\n<li>\u578b\u5225\u932f\u8aa4\uff1a\u4f60\u53ef\u80fd\u4f7f\u7528\u4e86\u932f\u8aa4\u7684\u578b\u5225\u3002<\/li>\n<li>\u4e0a\u4e0b\u6587\u4e0d\u6b63\u78ba\uff1a\u4f60\u53ef\u80fd\u5728\u4e0d\u6b63\u78ba\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u4e86\u5c6c\u6027\u6216\u65b9\u6cd5\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>\u89e3\u6c7a\u6b64\u932f\u8aa4\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<h3>1. \u6aa2\u67e5\u5c6c\u6027\u6216\u65b9\u6cd5\u540d\u7a31<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u6240\u4f7f\u7528\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u540d\u7a31\u662f\u6b63\u78ba\u7684\u3002\u9019\u53ef\u4ee5\u901a\u904e\u67e5\u770b\u5b98\u65b9\u6587\u6a94\u6216\u578b\u5225\u7684\u5b9a\u7fa9\u4f86\u78ba\u8a8d\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u4f60\u6709\u4e00\u500b\u985e\u5225 <code>Person<\/code>\uff0c\u800c\u4f60\u5617\u8a66\u8a2a\u554f <code>person.age<\/code>\uff0c\u4f46 <code>Person<\/code> \u985e\u5225\u4e2d\u4e26\u4e0d\u5b58\u5728 <code>age<\/code> \u5c6c\u6027\u3002\u9019\u6642\uff0c\u4f60\u9700\u8981\u6aa2\u67e5\u4e26\u78ba\u8a8d\u6b63\u78ba\u7684\u5c6c\u6027\u540d\u7a31\u3002<\/p>\n<h3>2. \u78ba\u8a8d\u578b\u5225\u7684\u6b63\u78ba\u6027<\/h3>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u5c6c\u6027\uff0c\u4f46\u8b8a\u6578\u7684\u578b\u5225\u8207\u4f60\u9810\u671f\u7684\u4e0d\u540c\uff0c\u9019\u4e5f\u6703\u5c0e\u81f4\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u500b\u6574\u6578\u985e\u578b\u7684\u8b8a\u6578\uff0c\u537b\u8aa4\u4ee5\u70ba\u5b83\u662f\u4e00\u500b\u5b57\u4e32\u985e\u578b\uff0c\u9019\u5c07\u7121\u6cd5\u8a2a\u554f\u5b57\u4e32\u7279\u6709\u7684\u5c6c\u6027\u3002<\/p>\n<h3>3. \u4f7f\u7528\u53ef\u9078\u578b\u5225\u4f86\u907f\u514d\u932f\u8aa4<\/h3>\n<p>\u5c0d\u65bc\u53ef\u9078\u578b\u5225\uff0c\u4f7f\u7528\u6642\u52d9\u5fc5\u6aa2\u67e5\u5b83\u5011\u662f\u5426\u70ba<code>nil<\/code>\u3002\u9019\u53ef\u4ee5\u901a\u904e\u53ef\u9078\u7d81\u5b9a\u6216\u5f37\u5236\u89e3\u5305\u4f86\u5b8c\u6210\u3002\u53ef\u9078\u7d81\u5b9a\u7684\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>if let person = optionalPerson {\n    print(person.name) \/\/ \u78ba\u4fdd name \u5c6c\u6027\u5b58\u5728\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u82e5\u4f60\u4ecd\u7136\u7121\u6cd5\u89e3\u6c7a\u554f\u984c\uff0c\u8003\u616e\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 Xcode \u7684\u7de8\u8b6f\u5668\u63d0\u793a\uff0c\u9019\u6709\u52a9\u65bc\u8b58\u5225\u554f\u984c\u6240\u5728\u3002<\/li>\n<li>\u53c3\u8003 Swift \u7684\u5b98\u65b9\u6587\u6a94\uff0c\u78ba\u8a8d\u4f60\u6b63\u5728\u4f7f\u7528\u7684\u578b\u5225\u53ca\u5176\u5c6c\u6027\u3002<\/li>\n<li>\u67e5\u770b\u958b\u767c\u8005\u793e\u7fa4\uff0c\u5982 Stack Overflow\uff0c\u5c0b\u6c42\u76f8\u4f3c\u554f\u984c\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u638c\u63e1\u5982\u4f55\u89e3\u6c7a\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u932f\u8aa4\uff0c\u5c07\u5c0d\u4f60\u7684 Swift \u958b\u767c\u6280\u8853\u6709\u5f88\u5927\u5e6b\u52a9\u3002\u7406\u89e3 Swift \u7684\u578b\u5225\u7cfb\u7d71\u548c\u5c6c\u6027\u8abf\u7528\u5c07\u4f7f\u4f60\u7684\u4ee3\u78bc\u66f4\u52a0\u7a69\u5065\uff0c\u6e1b\u5c11\u932f\u8aa4\u767c\u751f\u7684\u6a5f\u6703\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u932f\u8aa4\u901a\u5e38\u53ef\u4ee5\u901a\u904e\u6aa2\u67e5\u5c6c\u6027\u540d\u7a31\u548c\u578b\u5225\u4f86\u89e3\u6c7a\u3002\u9075\u5faa\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u4e26\u61c9\u7528\u6700\u4f73\u5be6\u8e10\uff0c\u5c07\u80fd\u5920\u6709\u6548\u6e1b\u5c11\u6b64\u985e\u932f\u8aa4\u7684\u767c\u751f\u3002\u5982\u679c\u554f\u984c\u4ecd\u7136\u5b58\u5728\uff0c\u4e0d\u59a8\u5c0b\u6c42\u5c08\u696d\u7684 Swift \u958b\u767c\u8005\u7684\u5e6b\u52a9\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u70ba\u4ec0\u9ebc\u6703\u51fa\u73fe\u300cValue of type &#8216;X&#8217; has no member &#8216;Y&#8217;\u300d\u932f\u8aa4\uff1f<\/h3>\n<p>A1: \u8a72\u932f\u8aa4\u901a\u5e38\u56e0\u70ba\u4f60\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u9020\u6210\uff0c\u53ef\u80fd\u662f\u56e0\u70ba\u62fc\u5beb\u932f\u8aa4\u6216\u578b\u5225\u4e0d\u6b63\u78ba\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u578b\u5225\u662f\u5426\u64c1\u6709\u67d0\u500b\u5c6c\u6027\u6216\u65b9\u6cd5\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u67e5\u95b1 Swift \u7684\u5b98\u65b9\u6587\u6a94\uff0c\u6216\u4f7f\u7528 Xcode \u7684\u81ea\u52d5\u88dc\u5168\u529f\u80fd\u4f86\u78ba\u8a8d\u578b\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<h3>Q3: \u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e6b\u52a9\u6211\u8abf\u8a66 Swift \u932f\u8aa4\uff1f<\/h3>\n<p>A3: Xcode \u672c\u8eab\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u8abf\u8a66\u5de5\u5177\uff0c\u9084\u6709 SwiftLint \u7b49\u5de5\u5177\u53ef\u4ee5\u5e6b\u52a9\u6aa2\u67e5\u4ee3\u78bc\u898f\u7bc4\u548c\u6f5b\u5728\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\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-4205","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-15P","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4205","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=4205"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4205\/revisions"}],"predecessor-version":[{"id":4206,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4205\/revisions\/4206"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}