{"id":4197,"date":"2025-06-02T11:55:01","date_gmt":"2025-06-05T04:21:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4197"},"modified":"2025-06-05T11:55:01","modified_gmt":"2025-06-05T04:21:00","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocolconformance%e9%8c%af%e8%aa%a4-3","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-3\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4\u53ca\u89e3\u6c7a\u65b9\u6cd5\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Protocol, Error Parsing, Swift \u6559\u5b78, Protocol Conformance\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4\u53ca\u89e3\u6c7a\u65b9\u6cd5<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f46\u5728\u958b\u767c\u904e\u7a0b\u4e2d\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\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u8a0a\u606f\u3002\u672c\u6587\u5c07\u6df1\u5165\u89e3\u91cb\u9019\u500b\u932f\u8aa4\u7684\u542b\u7fa9\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u53ca\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u8655\u7406\u9019\u985e\u554f\u984c\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Protocol\uff1f<\/h2>\n<p>\u5728 Swift \u4e2d\uff0cProtocol \u662f\u4e00\u7a2e\u7279\u6b8a\u985e\u578b\uff0c\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u65b9\u6cd5\u3001\u5c6c\u6027\u53ca\u5176\u4ed6\u8981\u6c42\uff0c\u4f9b\u5176\u4ed6\u985e\u578b\u5be6\u4f5c\u3002Protocol \u53ef\u4ee5\u88ab\u8996\u70ba\u4e00\u7a2e\u85cd\u5716\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u7d44\u884c\u70ba\uff0c\u4f9b\u4e0d\u540c\u7684\u985e\u578b\u4f86\u9075\u5faa\u548c\u5be6\u4f5c\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4\uff1f<\/h2>\n<p>\u7576\u4f60\u5617\u8a66\u5c07\u67d0\u500b\u985e\u578b\u8f49\u63db\u70ba Protocol \u6642\uff0c\u5982\u679c\u8a72\u985e\u578b\u672a\u5be6\u4f5c Protocol \u4e2d\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\u901a\u5e38\u662f\u56e0\u70ba\u985e\u578b\u7f3a\u5c11\u5fc5\u8981\u7684\u65b9\u6cd5\u3001\u5c6c\u6027\u6216\u5176\u4ed6\u8981\u6c42\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>\u8981\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u4f60\u9700\u8981\u78ba\u8a8d\u4e26\u5be6\u4f5c Protocol \u4e2d\u7684\u6240\u6709\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6b65\u9a5f\u548c\u7bc4\u4f8b\uff0c\u53ef\u4ee5\u5e6b\u52a9\u4f60\u89e3\u6c7a\u6b64\u554f\u984c\uff1a<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u6aa2\u67e5 Protocol \u7684\u8981\u6c42<\/h3>\n<p>\u9996\u5148\uff0c\u67e5\u770b Protocol \u7684\u5b9a\u7fa9\uff0c\u78ba\u8a8d\u6240\u9700\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\u3002<\/p>\n<p>&#8220;`swift<br \/>\nprotocol Vehicle {<br \/>\n    var wheels: Int { get }<br \/>\n    func drive()<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u5be6\u4f5c Protocol<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728\u4f60\u7684\u985e\u578b\u4e2d\u5be6\u4f5c Protocol \u7684\u6240\u6709\u8981\u6c42\u3002<\/p>\n<p>&#8220;`swift<br \/>\nclass Car: Vehicle {<br \/>\n    var wheels: Int = 4<\/p>\n<p>    func drive() {<br \/>\n        print(&#8220;Driving a car with \\(wheels) wheels.&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u6b65\u9a5f 3\uff1a\u4f7f\u7528 Extension \u5be6\u4f5c Protocol<\/h3>\n<p>\u5982\u679c\u4f60\u7684\u985e\u578b\u8907\u96dc\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 Swift \u7684 extension \u4f86\u5be6\u4f5c Protocol\u3002<\/p>\n<p>&#8220;`swift<br \/>\nextension Car {<br \/>\n    func honk() {<br \/>\n        print(&#8220;Honk! Honk!&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u4f60\u4ecd\u7136\u9047\u5230\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\u90fd\u6b63\u78ba\u5be6\u4f5c\u3002<br \/>\n&#8211; \u6aa2\u67e5\u8fd4\u56de\u985e\u578b\u548c\u53c3\u6578\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<br \/>\n&#8211; \u78ba\u8a8d\u662f\u5426\u6709\u4efb\u4f55\u907a\u6f0f\u7684\u8981\u6c42\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>Protocol \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u5b9a\u7fa9\u884c\u70ba\uff0c\u9084\u53ef\u4ee5\u901a\u904e\u5354\u5b9a\u64f4\u5c55\u4f86\u589e\u5f37\u529f\u80fd\u3002\u9019\u6a23\u53ef\u4ee5\u4f7f\u4ee3\u78bc\u66f4\u52a0\u9748\u6d3b\u4e14\u53ef\u91cd\u7528\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c07 Protocol \u8207\u6cdb\u578b\u7d50\u5408\u4f7f\u7528\uff0c\u4f86\u5be6\u73fe\u66f4\u901a\u7528\u7684\u529f\u80fd\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u662f Swift \u4e2d\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u8a0a\u606f\uff0c\u8868\u793a\u8a72\u985e\u578b\u672a\u5be6\u4f5c Protocol \u7684\u6240\u6709\u8981\u6c42\u3002\u901a\u904e\u6aa2\u67e5\u548c\u5be6\u4f5c Protocol \u7684\u6240\u6709\u8981\u6c42\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u4e26\u63d0\u5347\u4f60\u7684 Swift \u7de8\u7a0b\u80fd\u529b\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5224\u65b7\u6211\u7684\u985e\u578b\u662f\u5426\u7b26\u5408 Protocol\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u6aa2\u67e5\u4f60\u7684\u985e\u578b\u662f\u5426\u5be6\u4f5c\u4e86 Protocol \u4e2d\u5b9a\u7fa9\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\u3002\u5982\u679c\u6709\u7f3a\u5931\uff0c\u7de8\u8b6f\u5668\u6703\u63d0\u793a\u76f8\u61c9\u7684\u932f\u8aa4\u8a0a\u606f\u3002<\/p>\n<h3>Q2: Protocol \u53ef\u4ee5\u7e7c\u627f\u55ce\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0cProtocol \u53ef\u4ee5\u7e7c\u627f\u5176\u4ed6 Protocol\uff0c\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u64f4\u5c55\u5176\u8981\u6c42\uff0c\u8b93\u66f4\u591a\u985e\u578b\u5be6\u4f5c\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u4f7f\u7528 Protocol \u4f86\u5be6\u73fe\u591a\u578b\uff1f<\/h3>\n<p>A3: \u4f60\u53ef\u4ee5\u4f7f\u7528 Protocol \u4f5c\u70ba\u51fd\u6578\u7684\u53c3\u6578\u985e\u578b\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u4e0d\u540c\u7684\u985e\u578b\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u9032\u884c\u8655\u7406\uff0c\u5be6\u73fe\u591a\u578b\u7684\u6548\u679c\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-4197","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-15H","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4197","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=4197"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4197\/revisions"}],"predecessor-version":[{"id":4198,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4197\/revisions\/4198"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}