{"id":4075,"date":"2025-05-30T11:18:24","date_gmt":"2025-06-05T04:06:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4075"},"modified":"2025-06-05T11:18:24","modified_gmt":"2025-06-05T04:06:24","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocolconformance%e9%8c%af%e8%aa%a4","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocolconformance%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4\uff1a\u89e3\u6c7a\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Protocol, Error Parsing, Protocol Conformance, Swift Tutorial\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4<\/h1>\n<p>\u5728 Swift \u958b\u767c\u4e2d\uff0c\u9047\u5230\u932f\u8aa4\u8a0a\u606f\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u662f\u5e38\u898b\u7684\u6311\u6230\u4e4b\u4e00\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u70ba\u60a8\u63d0\u4f9b 2025 \u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u60a8\u7406\u89e3\u548c\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u8b93\u60a8\u5728 Swift \u7a0b\u5f0f\u8a2d\u8a08\u4e0a\u66f4\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f Swift Protocol\uff1f<\/h2>\n<p>\u5728\u6df1\u5165\u4e86\u89e3\u932f\u8aa4\u8a0a\u606f\u4e4b\u524d\uff0c\u9996\u5148\u8981\u7406\u89e3 Swift Protocol \u7684\u6982\u5ff5\u3002Protocol \u662f Swift \u4e2d\u7684\u4e00\u7a2e\u5f37\u5927\u7279\u6027\uff0c\u5141\u8a31\u4f60\u5b9a\u7fa9\u4e00\u7d44\u884c\u70ba\uff0c\u5305\u542b\u65b9\u6cd5\u3001\u5c6c\u6027\u548c\u5176\u4ed6\u8981\u6c42\uff0c\u4f7f\u5f97\u4e0d\u540c\u7684\u985e\u5225\u6216\u7d50\u69cb\u53ef\u4ee5\u9075\u5faa\u9019\u4e9b\u884c\u70ba\u3002\u9019\u7a2e\u6a5f\u5236\u4fc3\u9032\u4e86\u4ee3\u78bc\u7684\u91cd\u7528\u8207\u9748\u6d3b\u6027\u3002<\/p>\n<h2>\u932f\u8aa4\u8a0a\u606f\u89e3\u91cb\uff1aType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;<\/h2>\n<p>\u7576\u60a8\u5617\u8a66\u8b93\u67d0\u500b\u985e\u5225\u6216\u7d50\u69cb\u9075\u5faa\u7279\u5b9a\u7684 Protocol\uff0c\u4f46\u6c92\u6709\u6eff\u8db3\u8a72 Protocol \u4e2d\u7684\u6240\u6709\u8981\u6c42\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u82e5 Protocol \u8981\u6c42\u5be6\u73fe\u67d0\u500b\u65b9\u6cd5\uff0c\u800c\u60a8\u537b\u672a\u63d0\u4f9b\uff0c\u5247\u6703\u89f8\u767c\u6b64\u932f\u8aa4\u3002<\/p>\n<h2>\u89e3\u6c7a\u65b9\u6848\uff1a\u5982\u4f55\u4fee\u5fa9 Protocol Conformance \u932f\u8aa4<\/h2>\n<p>\u8981\u89e3\u6c7a\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\uff0c\u8acb\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<p>1. **\u6aa2\u67e5 Protocol \u7684\u8981\u6c42**\uff1a\u9996\u5148\u67e5\u770b\u60a8\u60f3\u8981\u9075\u5faa\u7684 Protocol\uff0c\u78ba\u4fdd\u60a8\u4e86\u89e3\u5176\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\u8981\u6c42\u3002<br \/>\n2. **\u5be6\u73fe\u6240\u6709\u8981\u6c42**\uff1a\u5728\u60a8\u7684\u985e\u5225\u6216\u7d50\u69cb\u4e2d\uff0c\u5be6\u73fe Protocol \u4e2d\u6240\u9700\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4fee\u6b63\u6b64\u932f\u8aa4\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre class=\"brush: swift\">\n\/\/ \u5b9a\u7fa9\u4e00\u500b\u5354\u5b9a\nprotocol MyProtocol {\n    func doSomething()\n}\n\n\/\/ \u5b9a\u7fa9\u4e00\u500b\u985e\u5225\nclass MyClass {\n    \/\/ \u985e\u5225\u7684\u5176\u4ed6\u5c6c\u6027\u548c\u65b9\u6cd5\n}\n\n\/\/ \u5c07\u5354\u5b9a\u64f4\u5c55\u5230\u985e\u5225\nextension MyClass: MyProtocol {\n    func doSomething() {\n        \/\/ \u5be6\u73fe\u5354\u5b9a\u4e2d\u7684\u65b9\u6cd5\n        print(\"Doing something!\")\n    }\n}\n<\/pre>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b Protocol `MyProtocol`\uff0c\u4e26\u5728 `MyClass` \u4e2d\u5be6\u73fe\u4e86\u8a72 Protocol \u7684\u8981\u6c42\uff0c\u5f9e\u800c\u6d88\u9664\u4e86\u932f\u8aa4\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<p>\u5982\u679c\u60a8\u4ecd\u7136\u9047\u5230\u6b64\u932f\u8aa4\uff0c\u53ef\u4ee5\u53c3\u8003\u4ee5\u4e0b\u5e7e\u9ede\u9032\u884c\u6392\u9664\uff1a<\/p>\n<p>&#8211; **\u6aa2\u67e5\u65b9\u6cd5\u7c3d\u540d**\uff1a\u78ba\u4fdd\u60a8\u5be6\u73fe\u7684\u65b9\u6cd5\u7c3d\u540d\u6b63\u78ba\u7121\u8aa4\uff0c\u5305\u62ec\u53c3\u6578\u548c\u8fd4\u56de\u985e\u578b\u3002<br \/>\n&#8211; **\u4f7f\u7528\u9069\u7576\u7684\u8a2a\u554f\u4fee\u98fe\u7b26**\uff1a\u78ba\u4fdd\u60a8\u5be6\u73fe\u7684\u65b9\u6cd5\u662f `public` \u6216 `internal`\uff0c\u4ee5\u6eff\u8db3 Protocol \u7684\u8981\u6c42\u3002<br \/>\n&#8211; **\u6aa2\u67e5\u7e7c\u627f\u95dc\u4fc2**\uff1a\u5982\u679c\u60a8\u7684\u985e\u5225\u6216\u7d50\u69cb\u7e7c\u627f\u81ea\u5176\u4ed6\u985e\u5225\uff0c\u78ba\u8a8d\u7236\u985e\u5225\u4e2d\u6c92\u6709\u76f8\u540c\u540d\u7a31\u7684\u65b9\u6cd5\u5c0e\u81f4\u885d\u7a81\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\uff1aProtocol \u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528 Protocol \u4e0d\u50c5\u80fd\u63d0\u5347\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u589e\u5f37\u5176\u9748\u6d3b\u6027\u3002\u60a8\u53ef\u4ee5\u900f\u904e Protocol \u4f86\u5be6\u73fe\u591a\u578b\u6027\uff0c\u4f7f\u5f97\u4e0d\u540c\u7684\u985e\u5225\u53ef\u4ee5\u5171\u4eab\u76f8\u540c\u7684\u884c\u70ba\uff0c\u9019\u5728\u5927\u578b\u5c08\u6848\u4e2d\u7279\u5225\u6709\u7528\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u6df1\u5165\u89e3\u6790\u4e86 Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002\u900f\u904e\u7406\u89e3 Protocol \u7684\u6982\u5ff5\u53ca\u6b63\u78ba\u7684\u5be6\u73fe\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u9019\u985e\u554f\u984c\uff0c\u63d0\u5347\u60a8\u7684 Swift \u958b\u767c\u6280\u80fd\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u77e5\u9053\u4e00\u500b\u985e\u5225\u662f\u5426\u7b26\u5408\u67d0\u500b Protocol\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528 Swift \u7684 `is` \u95dc\u9375\u5b57\u4f86\u6aa2\u67e5\u4e00\u500b\u5be6\u4f8b\u662f\u5426\u9075\u5faa\u67d0\u500b Protocol\uff0c\u4f8b\u5982\uff1a`if instance is MyProtocol { &#8230; }`\u3002<\/p>\n<h3>Q2: Protocol \u53ef\u4ee5\u6709\u5c6c\u6027\u55ce\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0cProtocol \u53ef\u4ee5\u5b9a\u7fa9\u5c6c\u6027\u8981\u6c42\uff0c\u60a8\u9700\u5728\u9075\u5faa\u8a72 Protocol \u7684\u985e\u5225\u4e2d\u63d0\u4f9b\u9019\u4e9b\u5c6c\u6027\u7684\u5177\u9ad4\u5be6\u73fe\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5728 Protocol \u4e2d\u5b9a\u7fa9\u53ef\u9078\u65b9\u6cd5\uff1f<\/h3>\n<p>A3: \u5728 Swift \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `@objc` \u548c `optional` \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u53ef\u9078\u65b9\u6cd5\uff0c\u4f46\u9019\u50c5\u5728 Objective-C \u517c\u5bb9\u7684 Protocol \u4e2d\u6709\u6548\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u89e3\u91cbSwift\u4e2d\u7684ProtocolConformance\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u66f4\u6709\u6548\u5730\u4f7f\u7528Swift\u4e2d\u7684Protocols\u4f86\u69cb\u5efa\u61c9\u7528\u7a0b\u5e8f\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-4075","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-13J","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4075","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=4075"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4075\/revisions"}],"predecessor-version":[{"id":4076,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4075\/revisions\/4076"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}