{"id":512,"date":"2018-02-23T23:38:54","date_gmt":"2018-02-23T15:38:54","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e5%b8%b8%e9%87%8f\/"},"modified":"2018-02-23T23:38:54","modified_gmt":"2018-02-23T15:38:54","slug":"swift-%e5%b8%b8%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=512","title":{"rendered":"Swift \u5e38\u91cf"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u5e38\u91cf<\/h1>\n<p>\u5e38\u91cf\u4e00\u65e6\u8bbe\u5b9a\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5c31\u65e0\u6cd5\u6539\u53d8\u5176\u503c\u3002<\/p>\n<p>\u5e38\u91cf\u53ef\u4ee5\u662f\u4efb\u4f55\u7684\u6570\u636e\u7c7b\u578b\u5982\uff1a\u6574\u578b\u5e38\u91cf\uff0c\u6d6e\u70b9\u578b\u5e38\u91cf\uff0c\u5b57\u7b26\u5e38\u91cf\u6216\u5b57\u7b26\u4e32\u5e38\u91cf\u3002\u540c\u6837\u4e5f\u6709\u679a\u4e3e\u7c7b\u578b\u7684\u5e38\u91cf\uff1a<\/p>\n<p>\n\u5e38\u91cf\u7c7b\u4f3c\u4e8e\u53d8\u91cf\uff0c\u533a\u522b\u5728\u4e8e\u5e38\u91cf\u7684\u503c\u4e00\u65e6\u8bbe\u5b9a\u5c31\u4e0d\u80fd\u6539\u53d8\uff0c\u800c\u53d8\u91cf\u7684\u503c\u53ef\u4ee5\u968f\u610f\u66f4\u6539\u3002<\/p>\n<hr>\n<h2>\u5e38\u91cf\u58f0\u660e<\/h2>\n<p>\u5e38\u91cf\u4f7f\u7528\u5173\u952e\u5b57 <b>let<\/b> \u6765\u58f0\u660e\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>\nlet constantName = &lt;initial value&gt;\n<\/pre>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Swift \u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5e38\u91cf\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet constA = 42\nprint(constA)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n42\n<\/pre>\n<hr>\n<h2>\u7c7b\u578b\u6807\u6ce8<\/h2>\n<p>\u5f53\u4f60\u58f0\u660e\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u7684\u65f6\u5019\u53ef\u4ee5\u52a0\u4e0a\u7c7b\u578b\u6807\u6ce8\uff08type annotation\uff09\uff0c\u8bf4\u660e\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u4e2d\u8981\u5b58\u50a8\u7684\u503c\u7684\u7c7b\u578b\u3002\u5982\u679c\u8981\u6dfb\u52a0\u7c7b\u578b\u6807\u6ce8\uff0c\u9700\u8981\u5728\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u540d\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u5192\u53f7\u548c\u7a7a\u683c\uff0c\u7136\u540e\u52a0\u4e0a\u7c7b\u578b\u540d\u79f0\u3002\n<\/p>\n<pre>\nvar constantName:&lt;data type&gt; = &lt;optional initial value&gt;\n<\/pre>\n<p>\n\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u662f\u5b9e\u4f8b\u6f14\u793a\u4e86 Swift \u4e2d\u5e38\u91cf\u4f7f\u7528\u7c7b\u578b\u6807\u6ce8\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u5e38\u91cf\u5b9a\u4e49\u65f6\u5fc5\u987b\u521d\u59cb\u503c\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet constA = 42\nprint(constA)\n\nlet constB:Float = 3.14159\n\nprint(constB)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n42\n3.14159<\/pre>\n<hr>\n<h2>\u5e38\u91cf\u547d\u540d<\/h2>\n<p>\u5e38\u91cf\u7684\u547d\u540d\u53ef\u4ee5\u7531\u5b57\u6bcd\uff0c\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\u3002<\/p>\n<p>\u5e38\u91cf\u9700\u8981\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u59cb\u3002<\/p>\n<p>Swift \u662f\u4e00\u4e2a\u533a\u5206\u5927\u5c0f\u5199\u7684\u8bed\u8a00\uff0c\u6240\u4ee5\u5b57\u6bcd\u5927\u5199\u4e0e\u5c0f\u5199\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n<p>\u5e38\u91cf\u540d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684 Unicode \u5b57\u7b26\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet _const = \"Hello, Swift!\"\nprint(_const)\n\nlet \u4f60\u597d = \"\u4f60\u597d\u4e16\u754c\"\nprint(\u4f60\u597d)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nHello, Swift!\n\u4f60\u597d\u4e16\u754c\n<\/pre>\n<hr>\n<h2>\u5e38\u91cf\u8f93\u51fa<\/h2>\n<p>\u53d8\u91cf\u548c\u5e38\u91cf\u53ef\u4ee5\u4f7f\u7528 print\uff08swift 2 \u5c06 print \u66ff\u6362\u4e86 println\uff09 \u51fd\u6570\u6765\u8f93\u51fa\u3002<\/p>\n<p>\n\u5728\u5b57\u7b26\u4e32\u4e2d\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u4e0e\u53cd\u659c\u7ebf\u6765\u63d2\u5165\u5e38\u91cf\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet name = \"\u83dc\u9e1f\u6559\u7a0b\"\nlet site = \"http:\/\/www.runoob.com\"\n\nprint(\"\\(name)\u7684\u5b98\u7f51\u5730\u5740\u4e3a\uff1a\\(site)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\u83dc\u9e1f\u6559\u7a0b\u7684\u5b98\u7f51\u5730\u5740\u4e3a\uff1ahttp:\/\/www.runoob.com<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u5e38\u91cf \u5e38\u91cf\u4e00\u65e6\u8bbe\u5b9a\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5c31\u65e0\u6cd5\u6539\u53d8\u5176\u503c\u3002 \u5e38\u91cf\u53ef\u4ee5\u662f\u4efb\u4f55\u7684\u6570\u636e\u7c7b\u578b\u5982\uff1a&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=512\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,28],"tags":[74,119],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-mobile","category-swift","tag-ios","tag-swift","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=512"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/512\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}