{"id":6264,"date":"2024-07-10T11:39:35","date_gmt":"2024-07-10T03:39:35","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6264"},"modified":"2024-07-10T11:39:35","modified_gmt":"2024-07-10T03:39:35","slug":"swift-%e4%b8%ad%e7%9a%84%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e8%bf%90%e7%ae%97%e7%ac%a6%ef%bc%9aas%e3%80%81as-%e5%92%8c-as%ef%bc%81%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6264","title":{"rendered":"Swift \u4e2d\u7684\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1aas\u3001as? \u548c as\uff01\u533a\u522b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u975e\u539f\u521b\uff0cGemini \u95ee\u7b54\u751f\u6210<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Swift \u4e2d\u7684\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1aas\u3001as? \u548c as\uff01\u8be6\u89e3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Swift \u4e2d\uff0c<code class=\"\">as<\/code>\u3001<code class=\"\">as?<\/code> \u548c <code class=\"\">as!<\/code> \u90fd\u662f\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u578b\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u578b\u3002\u4f46\u662f\uff0c\u5b83\u4eec\u5728\u884c\u4e3a\u548c\u9002\u7528\u573a\u666f\u4e0a\u5b58\u5728\u7740\u4e00\u4e9b\u5173\u952e\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>as<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\">as<\/code> \u8fd0\u7b97\u7b26\u662f\u6700\u57fa\u672c\u7684\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\u3002\u5b83\u5c1d\u8bd5\u5c06\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\uff0c\u5982\u679c\u8f6c\u6362\u6210\u529f\u5219\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u503c\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u8fd4\u56de <code class=\"\">nil<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"123\"\nlet num = str as Int? \/\/ num = Optional(123)\n\nlet str2 = \"abc\"\nlet num2 = str2 as Int? \/\/ num2 = nil\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <code>as?<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\">as?<\/code> \u8fd0\u7b97\u7b26\u7c7b\u4f3c\u4e8e <code class=\"\">as<\/code>\uff0c\u4f46\u5b83\u66f4\u4f18\u96c5\u5730\u5904\u7406\u5931\u8d25\u7684\u8f6c\u6362\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5b83\u4f1a\u8fd4\u56de <code class=\"\">nil<\/code>\uff0c\u800c\u4e0d\u4f1a\u5f15\u53d1\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"123\"\nlet num = str as? Int \/\/ num = 123\n\nlet str2 = \"abc\"\nlet num2 = str2 as? Int \/\/ num2 = nil\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <code>as!<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\">as!<\/code> \u8fd0\u7b97\u7b26\u662f\u6700\u5f3a\u5236\u529b\u7684\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\u3002\u5b83\u5c1d\u8bd5\u5c06\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\uff0c\u5e76\u5047\u8bbe\u8f6c\u6362\u59cb\u7ec8\u4f1a\u6210\u529f\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5b83\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let str = \"123\"\nlet num = str as! Int \/\/ num = 123\n\nlet str2 = \"abc\"\n\/\/ let num2 = str2 as! Int \/\/ \u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f55\u65f6\u4f7f\u7528\u6bcf\u4e2a\u8fd0\u7b97\u7b26\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u60a8\u786e\u5b9a\u8f6c\u6362\u59cb\u7ec8\u4f1a\u6210\u529f\u4e14\u60f3\u8981\u76f4\u63a5\u62c6\u5305\u503c\u65f6\uff0c\u8bf7\u4f7f\u7528 <code class=\"\">as<\/code>\u3002<\/li>\n\n\n\n<li>\u5f53\u60a8\u4e0d\u786e\u5b9a\u8f6c\u6362\u662f\u5426\u4f1a\u6210\u529f\u4e14\u60f3\u8981\u4f18\u96c5\u5730\u5904\u7406\u5931\u8d25\u53ef\u80fd\u6027\u65f6\uff0c\u8bf7\u4f7f\u7528 <code class=\"\">as?<\/code>\u3002<\/li>\n\n\n\n<li>\u4ec5\u5728\u60a8\u7edd\u5bf9\u786e\u5b9a\u8f6c\u6362\u4f1a\u6210\u529f\u4e14\u53ef\u4ee5\u5904\u7406\u6f5c\u5728\u8fd0\u884c\u65f6\u9519\u8bef\u65f6\u624d\u4f7f\u7528 <code class=\"\">as!<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u822c\u6765\u8bf4\uff0c\u5efa\u8bae\u60a8<\/strong> <strong>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f7f\u7528 <code>as?<\/code><\/strong>\uff0c\u56e0\u4e3a\u5b83\u66f4\u5b89\u5168\u3001\u66f4\u7075\u6d3b\u3002\u4ec5\u5728\u60a8\u786e\u4fe1\u8f6c\u6362\u65f6\u624d\u4f7f\u7528 <code class=\"\">as<\/code>\uff0c\u4ec5\u5728\u60a8\u7edd\u5bf9\u786e\u4fe1\u5e76\u53ef\u4ee5\u5904\u7406\u6f5c\u5728\u8fd0\u884c\u65f6\u9519\u8bef\u65f6\u624d\u4f7f\u7528 <code class=\"\">as!<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u8868\u683c\u603b\u7ed3\u4e86\u4e09\u4e2a\u8fd0\u7b97\u7b26\u4e4b\u95f4\u7684\u5173\u952e\u533a\u522b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u8fd0\u7b97\u7b26<\/th><th>\u884c\u4e3a<\/th><th>\u4f7f\u7528\u6848\u4f8b<\/th><\/tr><tr><td><code>as<\/code><\/td><td>\u5c1d\u8bd5\u8f6c\u6362\u5e76\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u503c\u6216 <code>nil<\/code>\uff08\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff09\u3002<\/td><td>\u4ec5\u5728\u60a8\u786e\u5b9a\u8f6c\u6362\u4f1a\u6210\u529f\u4e14\u60f3\u8981\u76f4\u63a5\u62c6\u5305\u503c\u65f6\u4f7f\u7528\u3002<\/td><\/tr><tr><td><code>as?<\/code><\/td><td>\u5c1d\u8bd5\u8f6c\u6362\u5e76\u8fd4\u56de <code>nil<\/code>\uff08\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff09\u3002<\/td><td>\u5f53\u60a8\u4e0d\u786e\u5b9a\u8f6c\u6362\u662f\u5426\u4f1a\u6210\u529f\u4e14\u60f3\u8981\u4f18\u96c5\u5730\u5904\u7406\u5931\u8d25\u53ef\u80fd\u6027\u65f6\u4f7f\u7528\u3002<\/td><\/tr><tr><td><code>as!<\/code><\/td><td>\u5c1d\u8bd5\u8f6c\u6362\u5e76\u5047\u8bbe\u8f6c\u6362\u59cb\u7ec8\u4f1a\u6210\u529f\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/td><td>\u4ec5\u5728\u60a8\u7edd\u5bf9\u786e\u5b9a\u8f6c\u6362\u4f1a\u6210\u529f\u4e14\u53ef\u4ee5\u5904\u7406\u6f5c\u5728\u8fd0\u884c\u65f6\u9519\u8bef\u65f6\u4f7f\u7528\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0cGemini \u95ee\u7b54\u751f\u6210 Swift \u4e2d\u7684\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1aas\u3001as? \u548c as\uff01\u8be6\u89e3 \u5728 Swift &#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6264\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[66,67],"class_list":["post-6264","post","type-post","status-publish","format-standard","hentry","category-5","tag-ios","tag-swift"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6264"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6264\/revisions"}],"predecessor-version":[{"id":6265,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6264\/revisions\/6265"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}