{"id":4115,"date":"2025-06-02T12:02:44","date_gmt":"2025-06-05T04:11:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4115"},"modified":"2025-06-05T12:02:44","modified_gmt":"2025-06-05T04:11:43","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocolconformance%e9%8c%af%e8%aa%a4-2","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-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6848"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Protocol, Error Parsing, Protocol Conformance, Swift \u6559\u5b78, Swift \u932f\u8aa4\u8655\u7406\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u932f\u8aa4\uff1a\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f46\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\uff0c\u958b\u767c\u8005\u5e38\u5e38\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\u8a0a\u606f\u3002\u5176\u4e2d\uff0c\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u662f\u5e38\u898b\u7684\u4e00\u7a2e\u932f\u8aa4\uff0c\u53ef\u80fd\u6703\u8b93\u521d\u5b78\u8005\u611f\u5230\u56f0\u60d1\u3002\u672c\u6587\u5c07\u6df1\u5165\u89e3\u6790\u9019\u500b\u932f\u8aa4\u8a0a\u606f\uff0c\u4e26\u63d0\u4f9b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u5e6b\u52a9\u4f60\u8f15\u9b06\u61c9\u5c0d\u9019\u4e00\u6311\u6230\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Swift Protocol\uff1f<\/h2>\n<p>\u5728\u89e3\u91cb\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u4e4b\u524d\uff0c\u6211\u5011\u9996\u5148\u9700\u8981\u4e86\u89e3 Swift \u4e2d\u7684 Protocol\u3002Swift Protocol \u662f\u4e00\u7a2e\u5b9a\u7fa9\u7279\u5b9a\u884c\u70ba\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8a31\u4f60\u5b9a\u7fa9\u4e00\u7d44\u65b9\u6cd5\u3001\u5c6c\u6027\u53ca\u5176\u4ed6\u8981\u6c42\uff0c\u4f9b\u5176\u4ed6\u985e\u5225\u6216\u7d50\u69cb\u5be6\u73fe\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u4fc3\u9032\u4e86\u7a0b\u5f0f\u7684\u9748\u6d3b\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<h2>\u7406\u89e3\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4<\/h2>\n<p>\u7576\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u985e\u5225\u6216\u7d50\u69cb\u5be6\u73fe\u4e00\u500b\u5354\u5b9a\uff08Protocol\uff09\u6642\uff0c\u5982\u679c\u672a\u6eff\u8db3\u5354\u5b9a\u7684\u6240\u6709\u8981\u6c42\uff0c\u5c31\u6703\u51fa\u73fe\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\u8a0a\u606f\u3002\u9019\u53ef\u80fd\u662f\u56e0\u70ba\u7f3a\u5c11\u5fc5\u8981\u7684\u65b9\u6cd5\u5be6\u73fe\u3001\u5c6c\u6027\u6216\u5c6c\u6027\u985e\u578b\u4e0d\u6b63\u78ba\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4\uff1f<\/h2>\n<p>\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u7684\u985e\u5225\u6216\u7d50\u69cb\u5be6\u73fe\u4e86\u5354\u5b9a\u4e2d\u7684\u6240\u6709\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u9ad4\u7684\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li><strong>\u6aa2\u67e5\u5354\u5b9a\u8981\u6c42\uff1a<\/strong>\u4ed4\u7d30\u95b1\u8b80\u5354\u5b9a\u7684\u5b9a\u7fa9\uff0c\u78ba\u4fdd\u5be6\u73fe\u4e86\u6240\u6709\u5fc5\u8981\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\u3002<\/li>\n<li><strong>\u4f7f\u7528 Extension\uff1a<\/strong>\u4f60\u53ef\u4ee5\u4f7f\u7528 Swift \u7684 <code>extension<\/code> \u4f86\u70ba\u985e\u5225\u6216\u7d50\u69cb\u6dfb\u52a0\u5354\u5b9a\u7684\u5be6\u73fe\u3002\u4f8b\u5982\uff1a<\/li>\n<pre><code>extension MyClass: MyProtocol {\n        func requiredMethod() {\n            \/\/ \u5be6\u4f5c\u4ee3\u78bc\n        }\n    }<\/code><\/pre>\n<li><strong>\u78ba\u8a8d\u5c6c\u6027\u985e\u578b\uff1a<\/strong>\u78ba\u4fdd\u6240\u6709\u5c6c\u6027\u90fd\u7b26\u5408\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u7684\u985e\u578b\u8981\u6c42\u3002<\/li>\n<\/ol>\n<h3>\u5be6\u4f5c\u7bc4\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5be6\u73fe\u5354\u5b9a\uff1a<\/p>\n<pre><code>protocol Vehicle {\n    var numberOfWheels: Int { get }\n    func drive()\n}\n\nclass Car: Vehicle {\n    var numberOfWheels: Int {\n        return 4\n    }\n    \n    func drive() {\n        print(\"Driving a car...\")\n    }\n}\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664\u5c0f\u6280\u5de7<\/h3>\n<p>\u5728\u9047\u5230\u6b64\u932f\u8aa4\u6642\uff0c\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u6aa2\u67e5\u5354\u5b9a\u662f\u5426\u6b63\u78ba\u547d\u540d\u3002<\/li>\n<li>\u78ba\u8a8d\u6240\u6709\u5fc5\u8981\u7684\u65b9\u6cd5\u90fd\u5df2\u5be6\u73fe\u4e14\u6b63\u78ba\u547d\u540d\u3002<\/li>\n<li>\u4f7f\u7528 Xcode \u7684\u81ea\u52d5\u88dc\u5168\u529f\u80fd\u5e6b\u52a9\u627e\u51fa\u7f3a\u5931\u7684\u5be6\u4f5c\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4e86\u89e3 Protocol Conformance \u7684\u57fa\u672c\u6982\u5ff5\u5f8c\uff0c\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u63a2\u7d22\u66f4\u8907\u96dc\u7684\u4f7f\u7528\u60c5\u5883\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u5354\u5b9a\u4f86\u8a2d\u8a08\u53ef\u64f4\u5c55\u7684 API\u3002<\/li>\n<li>\u5229\u7528\u5354\u5b9a\u4f86\u5be6\u73fe\u591a\u578b\u6027\uff0c\u5beb\u51fa\u66f4\u5177\u5f48\u6027\u7684\u7a0b\u5f0f\u78bc\u3002<\/li>\n<\/ul>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u6df1\u5165\u89e3\u6790\u4e86\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\u8a0a\u606f\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u548c\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u4f60\u8f15\u9b06\u89e3\u6c7a\u9019\u500b\u554f\u984c\u3002\u5982\u679c\u4f60\u4ecd\u6709\u7591\u554f\uff0c\u8acb\u96a8\u6642\u767c\u554f\uff01<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u77e5\u9053\u67d0\u500b\u985e\u5225\u662f\u5426\u5df2\u7b26\u5408\u5354\u5b9a\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u4f7f\u7528 Swift \u7684\u985e\u578b\u6aa2\u67e5\u529f\u80fd\uff0c\u4f8b\u5982 <code>if someObject is SomeProtocol<\/code> \u4f86\u6aa2\u67e5\u67d0\u500b\u7269\u4ef6\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u5354\u5b9a\u3002<\/p>\n<h3>Q2: \u5354\u5b9a\u548c\u7e7c\u627f\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>A2: \u5354\u5b9a\u5b9a\u7fa9\u4e86\u4e00\u7d44\u8981\u6c42\uff0c\u800c\u7e7c\u627f\u5247\u662f\u5f9e\u7236\u985e\u5225\u7372\u5f97\u5c6c\u6027\u548c\u65b9\u6cd5\u7684\u6a5f\u5236\u3002\u5354\u5b9a\u5141\u8a31\u591a\u500b\u985e\u5225\u5be6\u73fe\u76f8\u540c\u7684\u8981\u6c42\uff0c\u800c\u4e0d\u5fc5\u7e7c\u627f\u81ea\u540c\u4e00\u985e\u5225\u3002<\/p>\n<h3>Q3: \u6211\u53ef\u4ee5\u5728\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u5c6c\u6027\u55ce\uff1f<\/h3>\n<p>A3: \u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u5728\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u5c6c\u6027\uff0c\u4e26\u6307\u5b9a\u5b83\u5011\u662f\u53ef\u8b80\u7684\u6216\u53ef\u5beb\u7684\uff0c\u9019\u6a23\u5be6\u73fe\u8a72\u5354\u5b9a\u7684\u985e\u5225\u5c31\u5fc5\u9808\u5be6\u73fe\u9019\u4e9b\u5c6c\u6027\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\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-4115","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-14n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4115","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=4115"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4115\/revisions"}],"predecessor-version":[{"id":4116,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4115\/revisions\/4116"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}