{"id":4239,"date":"2025-05-30T11:40:35","date_gmt":"2025-06-05T04:27:35","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4239"},"modified":"2025-06-05T11:40:35","modified_gmt":"2025-06-05T04:27:35","slug":"%e4%ba%86%e8%a7%a3swift%e4%b8%ad%e7%9a%84protocolconformance%e9%8c%af%e8%aa%a4-4","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-4\/","title":{"rendered":"\u89e3\u6c7a Swift \u4e2d\u7684 Protocol Conformance \u932f\u8aa4\uff1a\u6700\u65b0 2025 \u5e74\u6559\u5b78\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, Protocol, Error Parsing, Swift \u6559\u5b78, Protocol Conformance \u932f\u8aa4\"><\/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\u5b83\u8b93\u958b\u767c\u8005\u80fd\u5920\u9ad8\u6548\u5730\u5275\u5efa iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u3002\u7136\u800c\uff0c\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\uff0c\u5176\u4e2d\u4e4b\u4e00\u4fbf\u662f\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u5168\u9762\u89e3\u91cb\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u8b93\u60a8\u7684 Swift \u7a0b\u5f0f\u78bc\u66f4\u52a0\u7a69\u5065\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Protocol\uff1f<\/h2>\n<p>Protocol \u662f Swift \u4e2d\u7684\u4e00\u7a2e\u7279\u6b8a\u985e\u578b\uff0c\u5b9a\u7fa9\u4e86\u4e00\u7d44\u53ef\u4ee5\u88ab\u5176\u4ed6\u985e\u578b\u5be6\u73fe\u7684\u65b9\u6cd5\u3001\u5c6c\u6027\u548c\u5176\u4ed6\u8981\u6c42\u3002\u9075\u5faa\u5354\u5b9a\u7684\u985e\u578b\u5fc5\u9808\u5be6\u73fe\u9019\u4e9b\u8981\u6c42\uff0c\u4ee5\u78ba\u4fdd\u5176\u884c\u70ba\u7684\u4e00\u81f4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u5354\u5b9a\u7bc4\u4f8b\uff0c\u8981\u6c42\u9075\u5faa\u8a72\u5354\u5b9a\u7684\u985e\u578b\u5fc5\u9808\u5be6\u73fe\u4e00\u500b\u540d\u70ba\u300csayHello()\u300d\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: swift\">\nprotocol Greetable {\n    func sayHello()\n}\n<\/pre>\n<p>\u4efb\u4f55\u60f3\u8981\u9075\u5faa Greetable \u5354\u5b9a\u7684\u985e\u578b\u90fd\u5fc5\u9808\u5be6\u73fe sayHello() \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: swift\">\nstruct Person: Greetable {\n    func sayHello() {\n        print(\"Hello!\")\n    }\n}\n<\/pre>\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\u4e00\u500b\u985e\u578b\u8a2d\u7f6e\u70ba\u9075\u5faa\u67d0\u500b\u5354\u5b9a\u6642\uff0c\u4f46\u8a72\u985e\u578b\u6c92\u6709\u5be6\u73fe\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u7684\u6240\u6709\u65b9\u6cd5\u3001\u5c6c\u6027\u548c\u5176\u4ed6\u8981\u6c42\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u985e\u578b\u8a2d\u7f6e\u70ba\u9075\u5faa Greetable \u5354\u5b9a\uff0c\u4f46\u8a72\u985e\u578b\u6c92\u6709\u5be6\u73fe sayHello() \u65b9\u6cd5\uff0c\u5c31\u6703\u51fa\u73fe\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<pre class=\"brush: swift\">\nstruct Animal {\n    \/\/ Animal \u6c92\u6709\u5be6\u73fe sayHello() \u65b9\u6cd5\n}\n\nlet animal = Animal()\nlet greetable: Greetable = animal \/\/ \u932f\u8aa4\uff1aType 'Animal' does not conform to protocol 'Greetable'\n<\/pre>\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\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u8a72\u985e\u578b\u5be6\u73fe\u4e86\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u7684\u6240\u6709\u65b9\u6cd5\u3001\u5c6c\u6027\u548c\u5176\u4ed6\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u89e3\u6c7a\u6b64\u932f\u8aa4\u7684\u5e7e\u7a2e\u65b9\u6cd5\uff1a<\/p>\n<p>1. **\u5be6\u73fe\u5354\u5b9a\u8981\u6c42**\uff1a\u5728 Animal \u985e\u578b\u4e2d\u5be6\u73fe sayHello() \u65b9\u6cd5\u3002<\/p>\n<pre class=\"brush: swift\">\nstruct Animal: Greetable {\n    func sayHello() {\n        print(\"Hello!\")\n    }\n}\n\nlet animal = Animal()\nlet greetable: Greetable = animal \/\/ \u6c92\u6709\u932f\u8aa4\n<\/pre>\n<p>2. **\u4f7f\u7528\u64f4\u5c55**\uff1a\u4f7f\u7528 Swift \u7684\u64f4\u5c55\u529f\u80fd\u5c07\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u7684\u65b9\u6cd5\u3001\u5c6c\u6027\u548c\u5176\u4ed6\u8981\u6c42\u6dfb\u52a0\u5230\u73fe\u6709\u7684\u985e\u578b\u4e2d\u3002<\/p>\n<pre class=\"brush: swift\">\nstruct Animal {\n    \/\/ Animal \u6c92\u6709\u5be6\u73fe sayHello() \u65b9\u6cd5\n}\n\nextension Animal: Greetable {\n    func sayHello() {\n        print(\"Hello!\")\n    }\n}\n\nlet animal = Animal()\nlet greetable: Greetable = animal \/\/ \u6c92\u6709\u932f\u8aa4\n<\/pre>\n<p>\u7e3d\u4e4b\uff0c\u70ba\u4e86\u4fee\u5fa9\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4\uff0c\u78ba\u8a8d\u985e\u578b\u6b63\u78ba\u5be6\u73fe\u5354\u5b9a\u8981\u6c42\uff0c\u6216\u4f7f\u7528\u64f4\u5c55\u529f\u80fd\u4f86\u9032\u884c\u9069\u7576\u7684\u5be6\u73fe\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<p>&#8211; **\u6aa2\u67e5\u5354\u5b9a\u8981\u6c42**\uff1a\u4ed4\u7d30\u6aa2\u67e5\u5354\u5b9a\u4e2d\u5b9a\u7fa9\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u78ba\u4fdd\u5df2\u6b63\u78ba\u5be6\u73fe\u3002<br \/>\n&#8211; **\u4f7f\u7528\u7de8\u8f2f\u5668\u63d0\u793a**\uff1a\u5927\u591a\u6578\u73fe\u4ee3\u7de8\u8f2f\u5668\u6703\u63d0\u4f9b\u5373\u6642\u932f\u8aa4\u63d0\u793a\uff0c\u5229\u7528\u9019\u9805\u529f\u80fd\u53ef\u4ee5\u5feb\u901f\u8fa8\u8b58\u554f\u984c\u6240\u5728\u3002<br \/>\n&#8211; **\u7c21\u5316\u4ee3\u78bc**\uff1a\u5982\u679c\u932f\u8aa4\u4fe1\u606f\u4e0d\u660e\u78ba\uff0c\u53ef\u4ee5\u5c07\u4ee3\u78bc\u7c21\u5316\uff0c\u9010\u6b65\u56de\u9000\uff0c\u627e\u51fa\u554f\u984c\u7684\u6839\u6e90\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; **\u5354\u5b9a\u64f4\u5c55**\uff1a\u5229\u7528\u5354\u5b9a\u64f4\u5c55\u4f86\u63d0\u4f9b\u9810\u8a2d\u5be6\u73fe\uff0c\u63d0\u9ad8\u4ee3\u78bc\u91cd\u7528\u6027\u3002<br \/>\n&#8211; **\u5354\u5b9a\u8207\u985e\u578b\u7d44\u5408**\uff1a\u5c07\u5354\u5b9a\u8207\u985e\u578b\u7d44\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5275\u5efa\u66f4\u9748\u6d3b\u7684 API\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a73\u7d30\u89e3\u91cb\u4e86 Swift \u4e2d\u7684\u300cType &#8216;X&#8217; does not conform to protocol &#8216;Y&#8217;\u300d\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002\u900f\u904e\u78ba\u4fdd\u985e\u578b\u9075\u5faa\u5354\u5b9a\u8981\u6c42\u6216\u4f7f\u7528\u64f4\u5c55\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u89e3\u6c7a\u9019\u4e9b\u932f\u8aa4\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u7a69\u5b9a\u6027\u8207\u53ef\u8b80\u6027\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\u578b\u662f\u5426\u9075\u5faa\u67d0\u500b\u5354\u5b9a\uff1f<\/h3>\n<p>A1: \u53ef\u4ee5\u4f7f\u7528 `is` \u6216 `as` \u95dc\u9375\u5b57\u4f86\u6aa2\u67e5\u985e\u578b\u662f\u5426\u9075\u5faa\u67d0\u500b\u5354\u5b9a\u3002\u4f8b\u5982\uff1a`if let greetableAnimal = animal as? Greetable { &#8230; }`<\/p>\n<h3>Q2: \u5982\u679c\u4e00\u500b\u985e\u578b\u4e0d\u9700\u8981\u5be6\u73fe\u6240\u6709\u5354\u5b9a\u8981\u6c42\uff0c\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u4f7f\u7528\u53ef\u9078\u8981\u6c42\uff0c\u9019\u6a23\u985e\u578b\u53ea\u9700\u8981\u5be6\u73fe\u5fc5\u8981\u7684\u65b9\u6cd5\uff0c\u5176\u4ed6\u5247\u53ef\u4ee5\u9078\u64c7\u6027\u5be6\u73fe\u3002<\/p>\n<h3>Q3: Swift \u4e2d\u7684\u5354\u5b9a\u548c\u4ecb\u9762\u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>A3: \u5728 Swift \u4e2d\uff0c\u5354\u5b9a\u548c\u4ecb\u9762\uff08\u5982\u5728\u5176\u4ed6\u8a9e\u8a00\u4e2d\uff09\u6709\u4e9b\u76f8\u4f3c\uff0c\u4f46 Swift \u7684\u5354\u5b9a\u53ef\u4ee5\u6709\u65b9\u6cd5\u9ed8\u8a8d\u5be6\u73fe\uff0c\u4e14\u652f\u63f4\u64f4\u5c55\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-4239","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-16n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4239","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=4239"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4239\/revisions"}],"predecessor-version":[{"id":4240,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4239\/revisions\/4240"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}