{"id":42,"date":"2021-09-09T17:02:23","date_gmt":"2021-09-09T09:02:23","guid":{"rendered":"https:\/\/yomisoft.com\/?p=42"},"modified":"2023-09-01T10:03:27","modified_gmt":"2023-09-01T02:03:27","slug":"swift5-0%e7%b2%be%e7%ae%80%e7%ac%94%e8%ae%b0-%e7%ac%ac2%e7%ab%a0-%e7%b1%bb%e5%9e%8b%e5%88%ab%e5%90%8d%e3%80%81%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e3%80%81%e5%8f%af%e9%80%89%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.yomisoft.com\/index.php\/2021\/09\/09\/swift5-0%e7%b2%be%e7%ae%80%e7%ac%94%e8%ae%b0-%e7%ac%ac2%e7%ab%a0-%e7%b1%bb%e5%9e%8b%e5%88%ab%e5%90%8d%e3%80%81%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e3%80%81%e5%8f%af%e9%80%89%e7%b1%bb%e5%9e%8b\/","title":{"rendered":"Swift5.0\u7cbe\u7b80\u7b14\u8bb0-\u7b2c2\u7ae0-\u7c7b\u578b\u522b\u540d\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u53ef\u9009\u7c7b\u578b"},"content":{"rendered":"<h2>1.1\u7c7b\u578b\u522b\u540d<\/h2>\n<p>\u600e\u4e48\u53bb\u7406\u89e3\u6240\u8c13\u7684\u7c7b\u578b\u522b\u540d\uff0c\u53ef\u4ee5\u8fd9\u4e48\u53bb\u60f3\uff0c\u6bd4\u5982\u4f60\u5728\u5916\u9762\u4eb2\u621a\u670b\u53cb\u4f1a\u53eb\u4f60\u4e00\u4e2a\u540d\u79f0\uff0c\u5728\u5bb6\u91cc\u9762\u7236\u6bcd\u4f1a\u53eb\u4f60\u53e6\u5916\u4e00\u4e2a\u540d\u79f0\uff0c\u519c\u6751\u91cc\u9762\u4e00\u4e9b\u4eba\u6240\u8bf4\u7684\u5c0f\u540d\u3001\u5927\u540d\u3002 :huaji13:<br \/>\n\u90a3\u4e48\u4e0b\u9762\u4ecb\u7ecd\u7c7b\u578b\u522b\u540d\u7684\u4f7f\u7528\uff1a<\/p>\n<pre class=\"hl\"><code class=\"\">typealias dog = String\nvar alpha:dog = \"hello\"\nvar beta:String = \"world\"\n\/\/\u5b57\u7b26\u4e32\u62fc\u63a5\nprint(alpha+\" \"+beta)\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\nhello world\n*\/\n<\/code><\/pre>\n<h2>1.2\u7c7b\u578b\u8f6c\u6362<\/h2>\n<p>\u7c7b\u578b\u8f6c\u6362\u5728\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u91cc\u9762\u90fd\u6709\u81ea\u5df1\u7684\u8f6c\u6362\u65b9\u5f0f\u3002[heimu]\u8fd9\u91cc\u6211\u8bb2\u4e86\u4e9b\u5bf9\u4ece\u6765\u6ca1\u6709\u63a5\u89e6\u8fc7\u4ee3\u7801\u7684\u4eba\u7406\u89e3\u8d77\u6765\u4f1a\u597d\u4e9b\u7684\u89e3\u91ca\uff0c\u4e0d\u8fc7\u6211\u8bb2\u7684\u6709\u70b9\u800c\u201c\u4e71\u201d\uff0c\u5177\u4f53\u4e86\u89e3\u5efa\u8bae\u4eb2\u81ea\u4e0a\u624b\u5199\u4e86\u624d\u4f1a\u77e5\u9053[\/heimu] :huaji9:<br \/>\n\u7ed3\u5408\u4e0a\u4e00\u7ae0\u7684\u6570\u636e\u7c7b\u578b\u6765\u770b\u4f1a\u597d\u5f88\u591a\uff0c\u4ee5\u4e0b\u5728Swift\u91cc\u9762\u8f6c\u6362\u7b80\u5355\u6f14\u793a\uff1a<\/p>\n<pre class=\"hl\"><code class=\"\">\n\/\/Int\u662f\u53ea\u5b58\u653e\u6574\u6570\u7684\uff0c\u90a3\u4e48\u4f60\u6709\u5c0f\u6570\u7684\u662f\u81ea\u7136\u4f1a\u81ea\u52a8\u53d8\u6210\u6574\u6570\u4e0d\u4f1a\u51fa\u73b0\u56db\u820d\u4e94\u5165\nprint(Int(100.55))\n\/\/Double\u53ea\u5b58\u653e\u5c0f\u6570\u7684\uff0c\u90a3\u4e48\u4f60\u7528\u6574\u6570\u53bb\u8f6c\u81ea\u7136\u4f1a\u81ea\u52a8\u5728\u9ed8\u8ba4\u51fa\u73b0\u5c0f\u6570\"10.0\"\nprint(Double(10))\n\/\/Bool\u53ea\u8981\u6709\u503c\u90a3\u4e48\u5c31\u4f1a\u662ftrue\uff0c\u800c0\u4ee3\u8868\u6ca1\u6709\u503c\u5c31\u4f1a\u662ffalse\uff0c\u4e0d\u7ba1\u662f\u8d1f\u6570\u8fd8\u662f\u6b63\u6570\u90fd\u4f1a\u53d8\u6210true\nprint(Bool(29.738))\nprint(Bool(0))\nprint(Bool(-12312))\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\ntrue\n100\n10.0\ntrue\nfalse\ntrue\n*\/\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u8bb2\u4e0b<code class=\"\">??<\/code>\uff0c\u56e0\u4e3a\u6709\u7684\u4eba\u4f1a\u95ee\u4e86\u5982\u679c\u6211\u7528\u5e38\u91cf\u6216\u53d8\u91cf\u6765\u5b9a\u4e49\u8981\u8f6c\u6362\u7684\u503c\u90a3\u4e48\u8be5\u600e\u4e48\u5199\u5450 :huaji13:<\/p>\n<pre class=\"hl\"><code class=\"\">let a = \"\u53cc\u51fb666\"\nprint(String(a))\nprint(Int(a) ?? 666)\nprint(Float(a) ?? 66.6)\nprint(Double(a) ?? 6.66)\nprint(Bool(a) ?? true)\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n\u53cc\u51fb666\n666\n66.6\n6.66\ntrue\n*\/\n<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u52a0\u4e86\u82f1\u6587\u7684\u4e24\u4e2a\u95ee\u53f7\uff0c\u5e94\u8be5\u8f6c\u6362\"\u53cc\u51fb666\"\u600e\u4e48\u53d8\u6210<code class=\"\">??<\/code> \u540e\u9762\u7684\u6570\u503c\u4e86\uff0c\u4f30\u8ba1\u806a\u660e\u7684\u4f60\u4e5f\u731c\u5230\u4e86\uff0c\u662f\u9ed8\u8ba4\u503c\uff0c\u5728\u4e24\u4e2a\u7c7b\u578b\u65e0\u6cd5\u4e92\u76f8\u8f6c\u6362\u7684\u65f6\u5019\u7ed9\u4e88\u4e86\u9ed8\u8ba4\u503c\u5219\u4f1a\u76f4\u63a5\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u800c\u4e0d\u662f\u6211\u4eec\u5b9a\u4e49\u7684\u503c\u3002<br \/>\n\u770b\u6587\u7ae0\u7684\u4f60\u5728\u60f3\u90a3\u6211\u5c31\u662f\u4e0d\u7ed9\u4f1a\u600e\u4e48\u6837\u00a0 [heimu]\u5c0f\u4f19\u5b50\u4e0d\u8981\u4e71\u641e\u4e8b\u4f1a\u7206\u70b8\u7684\uff0c\u61c2\u561b :huaji23:\u00a0[\/heimu]<\/p>\n<pre class=\"hl\"><code class=\"\">let a = \"\u53cc\u51fb666\"\nprint(String(a))\nprint(Int(a))\nprint(Float(a))\nprint(Double(a))\nprint(Bool(a))\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n\u53cc\u51fb666\nnil\nnil\nnil\nnil\n*\/\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u4f60\u7ed9\u5b57\u7b26\u4e32<code class=\"\">String<\/code>\u91cc\u9762\u5305\u542b\u4e86\u6570\u5b57\u3001\u6587\u5b57\u7684\u65f6\u5019\uff0c\u8f6c\u6362\u5230<code class=\"\">Int\u3001Float\u3001Double\u3001Bool<\/code>\u90fd\u4f1a\u76f4\u63a5\u53d8\u6210\u7a7a\u503c<code class=\"\">nil<\/code>\uff0c\u7eaf\u6587\u5b57\u8f6c\u6574\u578b\u3001\u6d6e\u70b9\u578b\u3001\u5e03\u5c14\u503c\u90fd\u4f1a\u53d8\u6210<code class=\"\">nil<\/code><br \/>\n[heimu]\u5f53\u7136\u4f60\u4ee3\u7801\u5199\u7684\u6709\u95ee\u9898\u7684\u65f6\u5019\u80af\u5b9a\u4f1abug\u62a5\u9519\u4ec0\u4e48\u7684\uff0c\u4e0d\u7136\u4f60\u4ee5\u4e3a\u4e3a\u4ec0\u4e48\u5199\u4ee3\u7801\u8981\u6709\u89c4\u8303[\/heimu]:huaji13:<br \/>\n\u4f46\u662f\u7eaf\u6587\u5b57\u662f\"true\"\u6216\"false\"\u8f6c\u5e03\u5c14\u503c\u6216\u7eaf\u6570\u5b57\u8f6c\u6362\u6574\u578b\u3001\u6d6e\u70b9\u578b\u5219\u4f1a\u63d0\u793a\u4f60<code class=\"\">Optional(\u8f6c\u6362\u503c\u6587\u5b57\u6216\u6570\u5b57)<\/code>\uff0c\u8fd9\u4e5f\u662f\u63a5\u4e0b\u6765\u6211\u4eec\u8981\u5b66\u7684Optional<\/p>\n<h2>1.3\u53ef\u9009\u7c7b\u578b<\/h2>\n<p>\u53ef\u9009\u7c7b\u578b\u662f\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u4f46\u4e5f\u662f\u975e\u5e38\u7b80\u5355\u7684\uff0c\u4ec0\u4e48\u662f\u53ef\u9009\u7c7b\u578b\u5450\uff0c\u6307\u7684\u662f\u4ed6\u53ef\u80fd\u6709\u503c\u53ef\u80fd\u6ca1\u6709\u503c.<br \/>\n\u5177\u4f53\u6211\u4eec\u4ee5\u4e0b\u9762\u6f14\u793a\uff1a<\/p>\n<pre class=\"hl\"><code class=\"\">var a = nil \/\/\u8fd9\u6837\u8bbe\u7f6e\u4e3a\u7a7a\u662f\u4e0d\u884c\u7684\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u7684\uff0c\u4f60\u9700\u8981\u660e\u786e\u6307\u5b9a\u4e00\u4e2a\u7c7b\u578b\nvar b:Int? = 10 \/\/\u8bbe\u7f6e\u4e3aInt\u53c8\u52a0\u4e86\u4e2a?\uff0c\u8868\u793a\u8bbe\u7f6e\u4e3a\u53ef\u9009\u7c7b\u578b\uff0c\u56e0\u4e3a\u4f60\u4e0d\u786e\u5b9ab\u8d4b\u503c\u7684\u5230\u5e95\u662f\u4e0d\u662fInt\u7c7b\u578b\nprint(b)\nprint(Int(\"100\"))\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\nOptional(10)\nOptional(100)\n*\/\n<\/code><\/pre>\n<p>\u5728\u53d8\u91cf\u8d4b\u503c\u6216\u662f\u53d8\u91cf\u8f6c\u578b\u7684\u65f6\u5019\u56e0\u4e3a\u4e0d\u786e\u5b9a\u4f60\u8f93\u5165\u7684\u5230\u5e95\u662f\u4ec0\u4e48\u4e1c\u897f\uff0c\u6240\u4ee5\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2aOptional\u53ef\u9009\u7c7b\u578b\uff0c\u800c\u4e4b\u524d\u4e0a\u9762\u6211\u4eec\u4e0d\u4e5f\u8bb2\u5230\u4e86<code class=\"\">??<\/code>\uff0c\u6240\u4ee5\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5c31\u8981\u7528\u5230\u4e86\uff0c\u5728\u4e0d\u786e\u5b9a\u4f60\u6240\u8f6c\u578b\u3001\u8d4b\u503c\u7684\u6570\u636e\u7ed9\u6307\u5b9a\u7684\u53d8\u91cf\u65f6\u5019\u5c31\u6700\u597d\u7528\u4e0a<code class=\"\">??<\/code>\u6765\u786e\u4fdd\u8be5\u53d8\u91cf\u662f\u6709\u4e00\u4e2a\u503c\u800c\u4e0d\u662f\u7a7a\u503c<code class=\"\">nil<\/code><br \/>\n[heimu]\u4f30\u8ba1\u53c8\u6709\u60f3\u641e\u4e8b\u60c5\u4e86\u8bf4\u6211var a:Int? = String(10) ?? 100\uff0c :pen: \u4f60.....\u6570\u636e\u7c7b\u578b\u4f60\u767d\u5b66\u4e86\uff0c\u54ea\u6709\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u6574\u578b\u6216\u6d6e\u70b9\u578b\u7684\uff0c\u518d\u8bf4\u4e86\u4f60\u6562\u8fd9\u6837\u5199\u7f16\u8bd1\u5668\u90fd\u4e0d\u6562\u8fd9\u6837\u8dd1\u4ee3\u7801\uff0c\u7acb\u9a6c\u63d0\u793a\u4f60\u4ee3\u7801\u8bed\u53e5\u9519\u8bef[\/heimu]<\/p>\n<pre class=\"hl\"><code class=\"\">var b:Int? = 10\nvar c:Int? = nil\nprint(b ?? 0)\nprint(c ?? 100)\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n10\n100\n*\/\n<\/code><\/pre>\n<p>\u5728\u5df2\u7ecf\u786e\u5b9a\u4e86\u8be5\u6307\u5b9a\u53d8\u91cf\u662f\u6709\u503c\u7684\u52a0\u4e00\u4e2a<code class=\"\">!<\/code>\u6765\u53d6\u51fa\u771f\u5b9e\u7684\u503c<\/p>\n<pre class=\"hl\"><code class=\"\">var b:Int? = 10\nvar c:Int? = nil\nprint(b!)\nprint(c)\n\/*\n*\u7f16\u8bd1\u5668\u6253\u5370\u7ed3\u679c\uff1a\n10\nnil\n*\/\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.1\u7c7b\u578b\u522b\u540d \u600e\u4e48\u53bb\u7406\u89e3\u6240\u8c13\u7684\u7c7b\u578b\u522b\u540d\uff0c\u53ef\u4ee5\u8fd9\u4e48\u53bb\u60f3\uff0c\u6bd4\u5982\u4f60\u5728\u5916\u9762\u4eb2\u621a\u670b\u53cb\u4f1a\u53eb\u4f60\u4e00\u4e2a\u540d\u79f0\uff0c\u5728\u5bb6\u91cc\u9762\u7236\u6bcd\u4f1a\u53eb\u4f60\u53e6\u5916\u4e00\u4e2a\u540d\u79f0\uff0c\u519c\u6751\u91cc &#8230;<\/p>\n","protected":false},"author":1,"featured_media":70,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[8,10,12],"class_list":["post-42","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3","tag-swift","tag-10","tag-12"],"_links":{"self":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":1,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions\/73"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/media\/70"}],"wp:attachment":[{"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yomisoft.com\/index.php\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}