{"id":4157,"date":"2025-06-04T11:35:42","date_gmt":"2025-06-05T04:16:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4157"},"modified":"2025-06-05T11:35:42","modified_gmt":"2025-06-05T04:16:42","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocols%e5%92%8ctypeconformance","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocols%e5%92%8ctypeconformance\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Swift \u4e2d\u7684 Protocol \u8207\u985e\u578b\u7b26\u5408\u6027\uff1a2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"Swift, Protocol, Error Parsing, Swift \u6559\u5b78, Swift \u6700\u65b0\u8a9e\u6cd5\"><\/p>\n<h1>\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\u4f60\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\u8a0a\u606f\uff0c\u5176\u4e2d\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u662f\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u3002\u9019\u500b\u932f\u8aa4\u8a0a\u606f\u53ef\u80fd\u6703\u8b93\u4f60\u611f\u5230\u56f0\u60d1\uff0c\u4f46\u5225\u64d4\u5fc3\uff01\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u8a73\u7d30\u89e3\u91cb\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u5206\u4eab\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528 Swift \u7684 Protocol \u7279\u6027\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Protocol\uff1f<\/h2>\n<p>\u5728\u6df1\u5165\u63a2\u8a0e\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u5148\u4e86\u89e3\u4ec0\u9ebc\u662f Protocol\u3002Protocol \u662f Swift \u4e2d\u7684\u4e00\u7a2e\u7279\u6b8a\u985e\u578b\uff0c\u5b83\u5b9a\u7fa9\u4e86\u4e00\u7d44\u8981\u6c42\uff0c\u8b93\u4e0d\u540c\u985e\u578b\u4e4b\u9593\u80fd\u5920\u9032\u884c\u4ea4\u4e92\u3002\u4f60\u53ef\u4ee5\u628a Protocol \u60f3\u50cf\u6210\u4e00\u500b\u85cd\u5716\uff0c\u5b9a\u7fa9\u4e86\u67d0\u4e9b\u884c\u70ba\uff0c\u4e26\u8981\u6c42\u5176\u4ed6\u985e\u578b\u9075\u5faa\u9019\u4e9b\u884c\u70ba\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\uff1f<\/h2>\n<p>\u7576\u4f60\u5617\u8a66\u4f7f\u7528\u67d0\u500b\u985e\u578b\u4f86\u5be6\u73fe\u4e00\u500b Protocol \u6642\uff0c\u5982\u679c\u8a72\u985e\u578b\u6c92\u6709\u9075\u5faa Protocol \u4e2d\u5b9a\u7fa9\u7684\u8981\u6c42\uff0c\u5247\u6703\u51fa\u73fe\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\u8a0a\u606f\u3002\u9019\u8868\u793a\u4f60\u5617\u8a66\u4f7f\u7528\u7684\u985e\u578b\u4e26\u672a\u6eff\u8db3 Protocol \u7684\u8981\u6c42\uff0c\u5c0e\u81f4\u7de8\u8b6f\u5931\u6557\u3002<\/p>\n<h2>\u89e3\u6c7a\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u7684\u6b65\u9a5f<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u6700\u4f73\u7684\u89e3\u6c7a\u65b9\u6cd5\u662f\u78ba\u4fdd\u4f60\u7684\u985e\u578b\u9075\u5faa Protocol \u4e2d\u7684\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u9ad4\u7684\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li><strong>\u6aa2\u67e5 Protocol \u7684\u5b9a\u7fa9\uff1a<\/strong>\u4f7f\u7528 Xcode \u7684 Quick Help \u529f\u80fd\u67e5\u770b Protocol \u7684\u8a73\u7d30\u5b9a\u7fa9\uff0c\u78ba\u8a8d\u54ea\u4e9b\u5c6c\u6027\u548c\u65b9\u6cd5\u662f\u5fc5\u9808\u5be6\u73fe\u7684\u3002<\/li>\n<li><strong>\u5be6\u4f5c\u6240\u6709\u8981\u6c42\uff1a<\/strong>\u78ba\u4fdd\u4f60\u7684\u985e\u578b\u5be6\u4f5c\u4e86 Protocol \u6240\u9700\u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/li>\n<pre><code>protocol Vehicle {\n    var numberOfWheels: Int { get }\n    func startEngine()\n}\n\nclass Car: Vehicle {\n    var numberOfWheels: Int = 4\n    \n    func startEngine() {\n        print(\"Engine started\")\n    }\n}<\/code><\/pre>\n<li><strong>\u4f7f\u7528 Extension \u4f86\u64f4\u5c55\u529f\u80fd\uff1a<\/strong>\u5982\u679c\u9700\u8981\uff0c\u5229\u7528 Swift \u7684 extension \u529f\u80fd\u4f86\u6dfb\u52a0 Protocol \u7684\u8981\u6c42\u3002\u4f8b\u5982\uff1a<\/li>\n<pre><code>extension Car {\n    func honk() {\n        print(\"Honk! Honk!\")\n    }\n}<\/code><\/pre>\n<\/ol>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<p>\u5982\u679c\u5728\u9075\u5faa\u4e0a\u9762\u7684\u6b65\u9a5f\u5f8c\u4ecd\u7136\u9047\u5230\u932f\u8aa4\uff0c\u8acb\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u6aa2\u67e5\u985e\u578b\u540d\u7a31\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u6c92\u6709\u62fc\u5beb\u932f\u8aa4\u3002<\/li>\n<li>\u78ba\u8a8d\u662f\u5426\u5df2\u7d93\u6b63\u78ba\u5c0e\u5165\u6240\u9700\u7684\u6a21\u7d44\u3002<\/li>\n<li>\u67e5\u770b\u662f\u5426\u6709\u4efb\u4f55\u7e7c\u627f\u95dc\u4fc2\u7684\u554f\u984c\uff0c\u4f8b\u5982\uff0c\u7236\u985e\u578b\u7684\u8981\u6c42\u672a\u88ab\u5b50\u985e\u578b\u5be6\u4f5c\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>Protocol \u4e0d\u50c5\u50c5\u7528\u65bc\u985e\u578b\u7b26\u5408\u6027\uff0c\u5b83\u5011\u9084\u53ef\u4ee5\u7528\u65bc\u8a2d\u8a08\u6a21\u5f0f\uff0c\u4f8b\u5982\u4ee3\u7406\u6a21\u5f0f\u548c\u89c0\u5bdf\u8005\u6a21\u5f0f\u3002\u9019\u610f\u5473\u8457\u4f60\u53ef\u4ee5\u5229\u7528 Protocol \u4f86\u69cb\u5efa\u66f4\u70ba\u9748\u6d3b\u548c\u53ef\u64f4\u5c55\u7684\u7a0b\u5f0f\u67b6\u69cb\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a73\u7d30\u89e3\u91cb\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\u89e3\u6c7a\u65b9\u6848\u3002\u7e3d\u4e4b\uff0c\u4f7f\u7528 Xcode \u7684 Quick Help \u4f86\u67e5\u770b Protocol \u4e2d\u7684\u8981\u6c42\uff0c\u78ba\u4fdd\u4f60\u7684\u985e\u578b\u9075\u5faa\u9019\u4e9b\u8981\u6c42\uff0c\u6216\u4f7f\u7528 Swift \u7684 extension \u529f\u80fd\u4f86\u6eff\u8db3 Protocol \u7684\u8981\u6c42\uff0c\u90fd\u662f\u6709\u6548\u7684\u65b9\u6cd5\u3002\u900f\u904e\u9019\u4e9b\u6700\u4f73\u5be6\u8e10\uff0c\u4f60\u5c07\u80fd\u5920\u66f4\u6709\u6548\u5730\u4f7f\u7528 Swift \u7684 Protocol \u7279\u6027\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\u985e\u578b\u662f\u5426\u7b26\u5408 Protocol \u7684\u8981\u6c42\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 Xcode \u7684 Quick Help \u4f86\u67e5\u770b Protocol \u7684\u5177\u9ad4\u8981\u6c42\uff0c\u4e26\u6aa2\u67e5\u4f60\u7684\u985e\u578b\u662f\u5426\u5be6\u4f5c\u4e86\u6240\u6709\u5fc5\u8981\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\u3002<\/p>\n<h3>2. Protocol \u662f\u5426\u53ef\u4ee5\u6709\u9810\u8a2d\u5be6\u4f5c\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u5f9e Swift 5 \u958b\u59cb\uff0cProtocol \u53ef\u4ee5\u4f7f\u7528 extension \u63d0\u4f9b\u9810\u8a2d\u5be6\u4f5c\uff0c\u9019\u6a23\u9075\u5faa\u8a72 Protocol \u7684\u985e\u578b\u53ef\u4ee5\u9078\u64c7\u8986\u5beb\u6216\u4f7f\u7528\u9810\u8a2d\u884c\u70ba\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728 Swift \u4e2d\u8655\u7406\u591a\u500b Protocol \u7684\u9075\u5faa\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u5728\u985e\u578b\u8072\u660e\u4e2d\u7528\u9017\u865f\u5206\u9694\u591a\u500b Protocol\uff0c\u9019\u6a23\u8a72\u985e\u578b\u5c31\u80fd\u540c\u6642\u9075\u5faa\u591a\u500b Protocol \u7684\u8981\u6c42\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>class MultiFunctionalDevice: Printer, Scanner {\n    \/\/ \u5be6\u4f5c Printer \u548c Scanner \u7684\u8981\u6c42\n}<\/code><\/pre>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u89e3\u91cbSwift\u4e2d\u7684Protocols\u548cTypeConformance\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7aType&#8217;X&#8217;doesnotconformtoprotocol&#8217;Y&#8217;\u932f\u8aa4\u7684\u65b9\u6cd5\u3002\u8b93\u4f60\u66f4\u52a0\u4e86\u89e3Swift\u4e2d\u7684Protocols\u548cTypeConformance\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u76f8\u95dc\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-4157","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-153","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4157","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=4157"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4157\/revisions"}],"predecessor-version":[{"id":4158,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4157\/revisions\/4158"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}