{"id":4201,"date":"2025-06-04T12:10:42","date_gmt":"2025-06-05T04:21:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4201"},"modified":"2025-06-05T12:10:42","modified_gmt":"2025-06-05T04:21:41","slug":"%e8%a7%a3%e6%9e%90swift%e9%8c%af%e8%aa%a4%ef%bc%9a%e7%84%a1%e6%b3%95%e5%b0%87%e5%9e%8b%e5%88%a5x%e8%bd%89%e6%8f%9b%e7%82%ba%e9%a0%90%e6%9c%9f%e7%9a%84%e5%8f%83%e6%95%b8%e5%9e%8b%e5%88%a5y-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e8%a7%a3%e6%9e%90swift%e9%8c%af%e8%aa%a4%ef%bc%9a%e7%84%a1%e6%b3%95%e5%b0%87%e5%9e%8b%e5%88%a5x%e8%bd%89%e6%8f%9b%e7%82%ba%e9%a0%90%e6%9c%9f%e7%9a%84%e5%8f%83%e6%95%b8%e5%9e%8b%e5%88%a5y-3\/","title":{"rendered":"Swift \u932f\u8aa4\u89e3\u6790\uff1a\u89e3\u6c7a\u300c\u7121\u6cd5\u5c07\u578b\u5225 &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, \u932f\u8aa4\u89e3\u6790, Cannot convert value of type 'X' to expected argument type 'Y', Swift \u6559\u5b78, Swift \u53ef\u9078\u985e\u578b, Swift \u985e\u578b\u8f49\u63db\"><\/p>\n<h1>Swift \u932f\u8aa4\u89e3\u6790\uff1a\u89e3\u6c7a\u300c\u7121\u6cd5\u5c07\u578b\u5225 &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u8c50\u5bcc\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u958b\u767c\u8005\u5e38\u5e38\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\uff0c\u5176\u4e2d\u4e4b\u4e00\u4fbf\u662f\u300c\u7121\u6cd5\u5c07\u503c &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u3002\u4e86\u89e3\u9019\u500b\u932f\u8aa4\u7684\u542b\u7fa9\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u5b83\u662f\u6bcf\u500b Swift \u958b\u767c\u8005\u5fc5\u5099\u7684\u6280\u80fd\u3002<\/p>\n<h2>\u932f\u8aa4\u89e3\u6790<\/h2>\n<p>\u9019\u500b\u932f\u8aa4\u7684\u610f\u601d\u662f\uff0c\u7576\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u503c\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u6642\uff0cSwift \u7121\u6cd5\u78ba\u8a8d\u8a72\u503c\u7b26\u5408\u9810\u671f\u7684\u985e\u578b\u3002\u9019\u901a\u5e38\u767c\u751f\u5728\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u5b57\u4e32\u8f49\u63db\u70ba\u6574\u6578\uff0c\u6216\u5c07\u6574\u6578\u8f49\u63db\u70ba\u5e03\u6797\u503c\u7b49\u60c5\u6cc1\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff1f<\/h2>\n<p>\u8981\u6709\u6548\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u6b63\u5728\u5617\u8a66\u5c07\u6b63\u78ba\u7684\u503c\u8f49\u63db\u70ba\u6b63\u78ba\u7684\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\u548c\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<h3>1. \u6aa2\u67e5\u8f38\u5165\u6578\u64da<\/h3>\n<p>\u78ba\u4fdd\u8f38\u5165\u7684\u6578\u64da\u7b26\u5408\u9810\u671f\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u7576\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u5b57\u4e32\u8f49\u63db\u70ba\u6574\u6578\u6642\uff0c\u4f60\u9700\u8981\u78ba\u8a8d\u8a72\u5b57\u4e32\u53ea\u5305\u542b\u6578\u5b57\u5b57\u7b26\u800c\u7121\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n<h3>2. \u4f7f\u7528\u53ef\u9078\u985e\u578b\uff08Optional Types\uff09<\/h3>\n<p>Swift \u7684\u53ef\u9078\u985e\u578b\u53ef\u4ee5\u8b93\u4f60\u5728\u5617\u8a66\u8f49\u63db\u503c\u6642\u66f4\u52a0\u5b89\u5168\u3002\u5b83\u80fd\u5920\u6aa2\u67e5\u8a72\u503c\u662f\u5426\u53ef\u4ee5\u88ab\u8f49\u63db\u70ba\u9810\u671f\u7684\u985e\u578b\uff0c\u5982\u679c\u4e0d\u80fd\u5247\u8fd4\u56de nil\u3002<\/p>\n<pre class=\"brush: swift\">\nlet numberString = \"123\"\nlet number = Int(numberString) \/\/ number is an Optional Int\n\nif let number = number {\n    print(\"\u8f49\u63db\u6210\u529f\uff0c\u6578\u5b57\u662f\uff1a\\(number)\") \/\/ number is an Int\n} else {\n    print(\"\u932f\u8aa4\uff1anumberString \u7121\u6cd5\u8f49\u63db\u70ba Int\")\n}\n<\/pre>\n<h3>3. \u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5982\u679c\u51fa\u73fe\u9019\u500b\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u8a8d\u8f38\u5165\u7684\u8cc7\u6599\u985e\u578b\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li>\u4f7f\u7528 print \u8a9e\u53e5\u4f86\u8abf\u8a66\u8f38\u5165\u6578\u64da\uff0c\u67e5\u770b\u5176\u771f\u5be6\u5167\u5bb9\u3002<\/li>\n<li>\u4f7f\u7528\u53ef\u9078\u985e\u578b\u4f86\u907f\u514d\u56e0\u985e\u578b\u4e0d\u5339\u914d\u800c\u5c0e\u81f4\u7684\u5d29\u6f70\u3002<\/li>\n<\/ul>\n<h3>4. \u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u5728\u8a31\u591a\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u9019\u7a2e\u985e\u578b\u7684\u932f\u8aa4\u6703\u983b\u7e41\u51fa\u73fe\u3002\u4f8b\u5982\uff0c\u5728\u7528\u6236\u8f38\u5165\u8868\u55ae\u6642\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c07\u7528\u6236\u8f38\u5165\u7684\u5b57\u4e32\u8f49\u63db\u70ba\u6578\u5b57\u3002\u5982\u679c\u4e0d\u52a0\u6aa2\u67e5\uff0c\u9019\u6703\u5c0e\u81f4\u61c9\u7528\u5d29\u6f70\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u53ef\u9078\u985e\u578b\u548c\u9069\u7576\u7684\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u300c\u7121\u6cd5\u5c07\u503c &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u662f\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u9019\u8868\u660e\u4f60\u5617\u8a66\u9032\u884c\u7684\u8f49\u63db\u4e0d\u7b26\u5408\u9810\u671f\u3002\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u78ba\u4fdd\u4f60\u6b63\u5728\u5c07\u6b63\u78ba\u7684\u503c\u8f49\u63db\u70ba\u6b63\u78ba\u7684\u985e\u578b\uff0c\u4e26\u5229\u7528 Swift \u7684\u53ef\u9078\u985e\u578b\u4f86\u5b89\u5168\u8655\u7406\u9019\u4e9b\u8f49\u63db\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5224\u65b7\u4e00\u500b\u5b57\u4e32\u662f\u5426\u53ef\u4ee5\u8f49\u63db\u70ba\u6574\u6578\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u50c5\u5305\u542b\u6578\u5b57\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528 Swift \u7684\u53ef\u9078\u578b\u5225\u4f86\u5617\u8a66\u8f49\u63db\uff0c\u4e26\u6aa2\u67e5\u7d50\u679c\u662f\u5426\u70ba nil\u3002<\/p>\n<h3>Q2: \u53ef\u9078\u985e\u578b\u5728 Swift \u4e2d\u6709\u4ec0\u9ebc\u597d\u8655\uff1f<\/h3>\n<p>A2: \u53ef\u9078\u985e\u578b\u8b93\u4f60\u80fd\u5920\u5b89\u5168\u5730\u8655\u7406\u53ef\u80fd\u70ba nil \u7684\u503c\uff0c\u5f9e\u800c\u907f\u514d\u56e0\u985e\u578b\u4e0d\u5339\u914d\u800c\u5c0e\u81f4\u7684\u5d29\u6f70\uff0c\u4e26\u63d0\u4f9b\u66f4\u5b89\u5168\u7684\u932f\u8aa4\u8655\u7406\u65b9\u5f0f\u3002<\/p>\n<h3>Q3: \u7576\u6211\u6536\u5230\u6b64\u932f\u8aa4\u6642\uff0c\u6211\u61c9\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>A3: \u7576\u9047\u5230\u6b64\u932f\u8aa4\u6642\uff0c\u9996\u5148\u6aa2\u67e5\u4f60\u7684\u8b8a\u6578\u985e\u578b\u53ca\u5176\u503c\uff0c\u78ba\u4fdd\u5b83\u5011\u7b26\u5408\u9810\u671f\u7684\u578b\u5225\u3002\u4f7f\u7528\u53ef\u9078\u985e\u578b\u4f86\u9032\u884c\u5b89\u5168\u7684\u8f49\u63db\uff0c\u4e26\u5229\u7528\u932f\u8aa4\u8655\u7406\u4f86\u8655\u7406\u53ef\u80fd\u51fa\u73fe\u7684\u554f\u984c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Swift\u8a9e\u8a00\u4e2d\u7684\u932f\u8aa4\u89e3\u6790\uff1a\u7121\u6cd5\u5c07\u578b\u5225&#8217;X&#8217;\u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225&#8217;Y&#8217;\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u8a72\u554f\u984c\u7684\u65b9\u6cd5\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-4201","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-15L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4201","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=4201"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4201\/revisions"}],"predecessor-version":[{"id":4202,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4201\/revisions\/4202"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}