{"id":4243,"date":"2025-05-30T11:45:42","date_gmt":"2025-06-05T04:28:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4243"},"modified":"2025-06-05T11:45:42","modified_gmt":"2025-06-05T04:28: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-4","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-4\/","title":{"rendered":"Swift \u932f\u8aa4\u89e3\u6790\uff1a\u5982\u4f55\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"},"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 \u578b\u5225\u8f49\u63db\"><\/p>\n<h1>Swift \u932f\u8aa4\u89e3\u6790\uff1a\u5982\u4f55\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<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u7136\u800c\uff0c\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\uff0c\u5176\u4e2d\u4e4b\u4e00\u662f\u300c\u7121\u6cd5\u5c07\u578b\u5225 &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u5e6b\u52a9\u4f60\u4e86\u89e3\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u3001\u89e3\u6c7a\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u932f\u8aa4\u539f\u56e0\u5206\u6790<\/h2>\n<p>\u9019\u500b\u932f\u8aa4\u7684\u610f\u601d\u662f\uff0c\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u503c\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u6642\uff0cSwift \u767c\u73fe\u8a72\u503c\u4e0d\u662f\u9810\u671f\u7684\u985e\u578b\u3002\u5e38\u898b\u7684\u60c5\u6cc1\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u8a66\u5716\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u6574\u6578\uff0c\u5b57\u4e32\u4e2d\u5305\u542b\u975e\u6578\u5b57\u5b57\u7b26\u3002<\/li>\n<li>\u5617\u8a66\u5c07\u6574\u6578\u8f49\u63db\u70ba\u5e03\u6797\u503c\uff0c\u9019\u5728\u908f\u8f2f\u4e0a\u662f\u4e0d\u6b63\u78ba\u7684\u3002<\/li>\n<li>\u4e0d\u76f8\u5bb9\u7684\u81ea\u8a02\u985e\u578b\u4e4b\u9593\u7684\u8f49\u63db\u3002<\/li>\n<\/ul>\n<h2>\u5982\u4f55\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff1f<\/h2>\n<p>\u8981\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\u5e38\u898b\u7684\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<h3>1. \u78ba\u8a8d\u8cc7\u6599\u578b\u5225<\/h3>\n<p>\u5728\u9032\u884c\u578b\u5225\u8f49\u63db\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u4f60\u6b63\u78ba\u5730\u4e86\u89e3\u6bcf\u500b\u8b8a\u6578\u7684\u578b\u5225\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5617\u8a66\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u6574\u6578\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u8a72\u5b57\u4e32\u53ea\u5305\u542b\u6578\u5b57\uff0c\u4e26\u4e14\u5728\u5be6\u65bd\u8f49\u63db\u4e4b\u524d\u9032\u884c\u6aa2\u67e5\u3002<\/p>\n<h3>2. \u4f7f\u7528\u53ef\u9078\u578b\u5225<\/h3>\n<p>Swift \u7684\u53ef\u9078\u578b\u5225\uff08optional types\uff09\u63d0\u4f9b\u4e86\u4e00\u500b\u5b89\u5168\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u578b\u5225\u8f49\u63db\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u53ef\u9078\u578b\u5225\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: swift\">\nlet str = \"123\"\nlet num = Int(str) \/\/ num is an optional Int\n\nif let num = num {\n    \/\/ num is not nil, so it can be safely used\n    print(num)\n} else {\n    print(\"\u8f49\u63db\u5931\u6557\uff0c\u8acb\u78ba\u8a8d\u5b57\u4e32\u662f\u5426\u5305\u542b\u6709\u6548\u6578\u5b57\u3002\")\n}\n<\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5617\u8a66\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u6574\u6578\u3002\u5982\u679c\u8a72\u5b57\u4e32\u4e0d\u5305\u542b\u6709\u6548\u7684\u6578\u5b57\uff0c\u5247 num \u5c07\u70ba nil\uff0c\u4e26\u5728\u8f49\u63db\u5931\u6557\u6642\u63d0\u4f9b\u53cb\u597d\u7684\u932f\u8aa4\u8a0a\u606f\u3002<\/p>\n<h3>3. \u932f\u8aa4\u8655\u7406<\/h3>\n<p>\u5728\u9032\u884c\u8cc7\u6599\u8f49\u63db\u6642\uff0c\u4f7f\u7528 Swift \u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\u4f86\u6355\u6349\u6f5b\u5728\u7684\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 do-catch \u8a9e\u53e5\u4f86\u8655\u7406\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u5065\u58ef\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9019\u7a2e\u578b\u5225\u8f49\u63db\u7684\u932f\u8aa4\u8655\u7406\u4e0d\u50c5\u9650\u65bc\u57fa\u672c\u578b\u5225\uff0c\u5728\u4f7f\u7528\u81ea\u8a02\u985e\u578b\u6216\u7d50\u69cb\u6642\uff0c\u540c\u6a23\u9700\u8981\u8b39\u614e\u8655\u7406\u3002\u78ba\u8a8d\u5404\u985e\u578b\u4e4b\u9593\u7684\u76f8\u5bb9\u6027\u662f\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u7a69\u5b9a\u904b\u884c\u7684\u95dc\u9375\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u7576\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u503c\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\uff0c\u800c\u8a72\u503c\u4e0d\u662f\u9810\u671f\u7684\u985e\u578b\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u300c\u7121\u6cd5\u5c07\u578b\u5225 &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\u3002\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u78ba\u4fdd\u4f60\u6b63\u5728\u5617\u8a66\u5c07\u6b63\u78ba\u7684\u503c\u8f49\u63db\u70ba\u6b63\u78ba\u7684\u985e\u578b\uff0c\u4e26\u5584\u7528\u53ef\u9078\u578b\u5225\u548c\u932f\u8aa4\u8655\u7406\u4f86\u589e\u5f37\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>Q1: \u70ba\u4ec0\u9ebc\u6703\u51fa\u73fe\u300c\u7121\u6cd5\u5c07\u578b\u5225 &#8216;X&#8217; \u8f49\u63db\u70ba\u9810\u671f\u7684\u53c3\u6578\u578b\u5225 &#8216;Y&#8217;\u300d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>A1: \u9019\u901a\u5e38\u662f\u56e0\u70ba\u4f60\u5617\u8a66\u5c07\u4e0d\u76f8\u5bb9\u7684\u578b\u5225\u9032\u884c\u8f49\u63db\uff0c\u4f8b\u5982\u5b57\u4e32\u8f49\u6574\u6578\u6642\u5b57\u4e32\u5305\u542b\u975e\u6578\u5b57\u5b57\u7b26\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5b89\u5168\u5730\u9032\u884c\u578b\u5225\u8f49\u63db\uff1f<\/h3>\n<p>A2: \u4f7f\u7528\u53ef\u9078\u578b\u5225\u53ef\u4ee5\u5728\u8f49\u63db\u6642\u6aa2\u67e5\u503c\u662f\u5426\u70ba nil\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u5728\u8f49\u63db\u5931\u6557\u6642\u7522\u751f\u932f\u8aa4\u3002<\/p>\n<h3>Q3: Swift \u4e2d\u7684\u578b\u5225\u8f49\u63db\u6709\u54ea\u4e9b\u6700\u4f73\u5be6\u8e10\uff1f<\/h3>\n<p>A3: \u78ba\u4fdd\u8cc7\u6599\u578b\u5225\u76f8\u5bb9\u3001\u4f7f\u7528\u53ef\u9078\u578b\u5225\u9032\u884c\u5b89\u5168\u8f49\u63db\uff0c\u4ee5\u53ca\u5728\u9700\u8981\u6642\u63a1\u7528\u932f\u8aa4\u8655\u7406\u6a5f\u5236\u4f86\u6355\u6349\u548c\u8655\u7406\u932f\u8aa4\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-4243","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-16r","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4243","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=4243"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4243\/revisions"}],"predecessor-version":[{"id":4244,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4243\/revisions\/4244"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}