{"id":509,"date":"2018-02-23T23:38:53","date_gmt":"2018-02-23T15:38:53","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b\/"},"modified":"2018-02-23T23:38:53","modified_gmt":"2018-02-23T15:38:53","slug":"swift-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=509","title":{"rendered":"Swift \u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u6570\u636e\u7c7b\u578b<\/h1>\n<p>\n\u5728\u6211\u4eec\u4f7f\u7528\u4efb\u4f55\u7a0b\u5e8f\u8bed\u8a00\u7f16\u7a0b\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u5404\u79cd\u6570\u636e\u7c7b\u578b\u6765\u5b58\u50a8\u4e0d\u540c\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u51b3\u5b9a\u4e86\u5982\u4f55\u5c06\u4ee3\u8868\u8fd9\u4e9b\u503c\u7684\u4f4d\u5b58\u50a8\u5230\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u4e2d\u3002\u5728\u58f0\u660e\u53d8\u91cf\u65f6\u4e5f\u53ef\u6307\u5b9a\u5b83\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>\u6240\u6709\u53d8\u91cf\u90fd\u5177\u6709\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u51b3\u5b9a\u80fd\u591f\u5b58\u50a8\u54ea\u79cd\u6570\u636e\u3002<\/p>\n<hr>\n<h2>\u5185\u7f6e\u6570\u636e\u7c7b\u578b<\/h2>\n<p>Swift \u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4e0b\u5217\u51fa\u4e86\u5e38\u7528\u4e86\u51e0\u79cd\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<h3>Int<\/h3>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u4f60\u4e0d\u9700\u8981\u4e13\u95e8\u6307\u5b9a\u6574\u6570\u7684\u957f\u5ea6\u3002Swift \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684\u6574\u6570\u7c7b\u578b<code>Int<\/code>\uff0c\u957f\u5ea6\u4e0e\u5f53\u524d\u5e73\u53f0\u7684\u539f\u751f\u5b57\u957f\u76f8\u540c\uff1a<\/p>\n<ul>\n<li>\u572832\u4f4d\u5e73\u53f0\u4e0a\uff0c<code>Int<\/code>\u548c<code>Int32<\/code>\u957f\u5ea6\u76f8\u540c\u3002<\/li>\n<li>\u572864\u4f4d\u5e73\u53f0\u4e0a\uff0c<code>Int<\/code>\u548c<code>Int64<\/code>\u957f\u5ea6\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p>\u9664\u975e\u4f60\u9700\u8981\u7279\u5b9a\u957f\u5ea6\u7684\u6574\u6570\uff0c\u4e00\u822c\u6765\u8bf4\u4f7f\u7528<code>Int<\/code>\u5c31\u591f\u4e86\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u4e00\u81f4\u6027\u548c\u53ef\u590d\u7528\u6027\u3002\u5373\u4f7f\u662f\u572832\u4f4d\u5e73\u53f0\u4e0a\uff0c<code>Int<\/code>\u53ef\u4ee5\u5b58\u50a8\u7684\u6574\u6570\u8303\u56f4\u4e5f\u53ef\u4ee5\u8fbe\u5230<code>-2,147,483,648<\/code>~<code>2,147,483,647<\/code>\uff0c\u5927\u591a\u6570\u65f6\u5019\u8fd9\u5df2\u7ecf\u8db3\u591f\u5927\u4e86\u3002<\/p>\n<p><a name=\"UInt\"><\/a><\/p>\n<h3>UInt<\/h3>\n<p>Swift \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684\u65e0\u7b26\u53f7\u7c7b\u578b<code>UInt<\/code>\uff0c\u957f\u5ea6\u4e0e\u5f53\u524d\u5e73\u53f0\u7684\u539f\u751f\u5b57\u957f\u76f8\u540c\uff1a<\/p>\n<ul>\n<li>\u572832\u4f4d\u5e73\u53f0\u4e0a\uff0c<code>UInt<\/code>\u548c<code>UInt32<\/code>\u957f\u5ea6\u76f8\u540c\u3002<\/li>\n<li>\u572864\u4f4d\u5e73\u53f0\u4e0a\uff0c<code>UInt<\/code>\u548c<code>UInt64<\/code>\u957f\u5ea6\u76f8\u540c\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a<br \/>\n\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528<code>UInt<\/code>\uff0c\u9664\u975e\u4f60\u771f\u7684\u9700\u8981\u5b58\u50a8\u4e00\u4e2a\u548c\u5f53\u524d\u5e73\u53f0\u539f\u751f\u5b57\u957f\u76f8\u540c\u7684\u65e0\u7b26\u53f7\u6574\u6570\u3002\u9664\u4e86\u8fd9\u79cd\u60c5\u51b5\uff0c\u6700\u597d\u4f7f\u7528<code>Int<\/code>\uff0c\u5373\u4f7f\u4f60\u8981\u5b58\u50a8\u7684\u503c\u5df2\u77e5\u662f\u975e\u8d1f\u7684\u3002\u7edf\u4e00\u4f7f\u7528<code>Int<\/code>\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\uff0c\u907f\u514d\u4e0d\u540c\u7c7b\u578b\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5e76\u4e14\u5339\u914d\u6570\u5b57\u7684\u7c7b\u578b\u63a8\u65ad\uff0c\u8bf7\u53c2\u8003<a href=\"\/#type_safety_and_type_inference\">\u7c7b\u578b\u5b89\u5168\u548c\u7c7b\u578b\u63a8\u65ad<\/a>\u3002<\/p>\n<\/blockquote>\n<h3>\u6d6e\u70b9\u6570<\/h3>\n<p>\u6d6e\u70b9\u6570\u662f\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u5b57\uff0c\u6bd4\u5982<code>3.14159<\/code>\uff0c<code>0.1<\/code>\u548c<code>-273.15<\/code>\u3002<\/p>\n<p>\u6d6e\u70b9\u7c7b\u578b\u6bd4\u6574\u6570\u7c7b\u578b\u8868\u793a\u7684\u8303\u56f4\u66f4\u5927\uff0c\u53ef\u4ee5\u5b58\u50a8\u6bd4<code>Int<\/code>\u7c7b\u578b\u66f4\u5927\u6216\u8005\u66f4\u5c0f\u7684\u6570\u5b57\u3002Swift \u63d0\u4f9b\u4e86\u4e24\u79cd\u6709\u7b26\u53f7\u6d6e\u70b9\u6570\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li><code>Double<\/code>\u8868\u793a64\u4f4d\u6d6e\u70b9\u6570\u3002\u5f53\u4f60\u9700\u8981\u5b58\u50a8\u5f88\u5927\u6216\u8005\u5f88\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u65f6\u8bf7\u4f7f\u7528\u6b64\u7c7b\u578b\u3002<\/li>\n<li><code>Float<\/code>\u8868\u793a32\u4f4d\u6d6e\u70b9\u6570\u3002\u7cbe\u5ea6\u8981\u6c42\u4e0d\u9ad8\u7684\u8bdd\u53ef\u4ee5\u4f7f\u7528\u6b64\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a<br \/>\n<code>Double<\/code>\u7cbe\u786e\u5ea6\u5f88\u9ad8\uff0c\u81f3\u5c11\u670915\u4f4d\u6570\u5b57\uff0c\u800c<code>Float<\/code>\u6700\u5c11\u53ea\u67096\u4f4d\u6570\u5b57\u3002\u9009\u62e9\u54ea\u4e2a\u7c7b\u578b\u53d6\u51b3\u4e8e\u4f60\u7684\u4ee3\u7801\u9700\u8981\u5904\u7406\u7684\u503c\u7684\u8303\u56f4\u3002<\/p>\n<\/blockquote>\n<h3>\u5e03\u5c14\u503c<\/h3>\n<p>Swift \u6709\u4e00\u4e2a\u57fa\u672c\u7684\u5e03\u5c14\uff08Boolean\uff09\u7c7b\u578b\uff0c\u53eb\u505aBool\u3002\u5e03\u5c14\u503c\u6307\u903b\u8f91\u4e0a\u7684\u503c\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u80fd\u662f\u771f\u6216\u8005\u5047\u3002Swift \u6709\u4e24\u4e2a\u5e03\u5c14\u5e38\u91cf\uff0ctrue\u548cfalse\u3002<\/p>\n<h3>\n\u5b57\u7b26\u4e32<\/h3>\n<p>\u5b57\u7b26\u4e32\u662f\u5b57\u7b26\u7684\u5e8f\u5217\u96c6\u5408\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\"Hello, World!\"<\/pre>\n<h3>\n\u5b57\u7b26<\/h3>\n<p>\u5b57\u7b26\u6307\u7684\u662f\u5355\u4e2a\u5b57\u6bcd\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\"C\"<\/pre>\n<h3>\u53ef\u9009\u7c7b\u578b<\/h3>\n<p>\u4f7f\u7528\u53ef\u9009\u7c7b\u578b\uff08optionals\uff09\u6765\u5904\u7406\u503c\u53ef\u80fd\u7f3a\u5931\u7684\u60c5\u51b5\u3002\u53ef\u9009\u7c7b\u578b\u8868\u793a\u6709\u503c\u6216\u6ca1\u6709\u503c\u3002<\/p>\n<hr>\n<h2>\u6570\u503c\u8303\u56f4<\/h2>\n<p>\u4e0b\u8868\u663e\u793a\u4e86\u4e0d\u540c\u53d8\u91cf\u7c7b\u578b\u5185\u5b58\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u53ca\u53d8\u91cf\u7c7b\u578b\u7684\u6700\u5927\u6700\u5c0f\u503c\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u5927\u5c0f\uff08\u5b57\u8282\uff09<\/th>\n<th>\u533a\u95f4\u503c<\/th>\n<\/tr>\n<tr>\n<td>Int8<\/td>\n<td>1 \u5b57\u8282<\/td>\n<td>-127 \u5230 127<\/td>\n<\/tr>\n<tr>\n<td>UInt8<\/td>\n<td>1 \u5b57\u8282<\/td>\n<td>0 \u5230 255<\/td>\n<\/tr>\n<tr>\n<td>Int32<\/td>\n<td>4 \u5b57\u8282<\/td>\n<td>-2147483648 \u5230 2147483647<\/td>\n<\/tr>\n<tr>\n<td>UInt32<\/td>\n<td>4 \u5b57\u8282<\/td>\n<td>0 \u5230 4294967295<\/td>\n<\/tr>\n<tr>\n<td>Int64<\/td>\n<td>8 \u5b57\u8282<\/td>\n<td>-9223372036854775808 \u5230 9223372036854775807<\/td>\n<\/tr>\n<tr>\n<td>UInt64<\/td>\n<td>8 \u5b57\u8282<\/td>\n<td>0 \u5230 18446744073709551615<\/td>\n<\/tr>\n<tr>\n<td>Float<\/td>\n<td>4 \u5b57\u8282<\/td>\n<td>1.2E-38 \u5230 3.4E+38 (~6 digits)<\/td>\n<\/tr>\n<tr>\n<td>Double<\/td>\n<td>8 \u5b57\u8282<\/td>\n<td>2.3E-308 \u5230 1.7E+308 (~15 digits)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2>\u7c7b\u578b\u522b\u540d<\/h2>\n<p>\u7c7b\u578b\u522b\u540d\u5bf9\u5f53\u524d\u7684\u7c7b\u578b\u5b9a\u4e49\u4e86\u53e6\u4e00\u4e2a\u540d\u5b57\uff0c\u7c7b\u578b\u522b\u540d\u901a\u8fc7\u4f7f\u7528 typealias \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>\ntypealias newname = type\n<\/pre>\n<p>\u4f8b\u5982\u4ee5\u4e0b\u5b9a\u4e49\u4e86 Int \u7684\u7c7b\u578b\u522b\u540d\u4e3a Feet\uff1a<\/p>\n<pre>\ntypealias Feet = Int\n<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u522b\u540d\u6765\u5b9a\u4e49\u53d8\u91cf\uff1a<\/p>\n<pre>\nimport Cocoa\n\ntypealias Feet = Int\nvar distance: Feet = 100\nprint(distance)\n<\/pre>\n<p>\u6211\u4eec\u4f7f\u7528 playground \u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n100\n<\/pre>\n<hr>\n<h2>\u7c7b\u578b\u5b89\u5168<\/h2>\n<p>Swift \u662f\u4e00\u4e2a\u7c7b\u578b\u5b89\u5168\uff08type safe\uff09\u7684\u8bed\u8a00\u3002<\/p>\n<p>\u7531\u4e8e Swift \u662f\u7c7b\u578b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u5b83\u4f1a\u5728\u7f16\u8bd1\u4f60\u7684\u4ee3\u7801\u65f6\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff08type checks\uff09\uff0c\u5e76\u628a\u4e0d\u5339\u914d\u7684\u7c7b\u578b\u6807\u8bb0\u4e3a\u9519\u8bef\u3002\u8fd9\u53ef\u4ee5\u8ba9\u4f60\u5728\u5f00\u53d1\u7684\u65f6\u5019\u5c3d\u65e9\u53d1\u73b0\u5e76\u4fee\u590d\u9519\u8bef\u3002<\/p>\n<pre>\nimport Cocoa\n\nvar varA = 42\nvarA = \"This is hello\"\nprint(varA)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u4f1a\u5728 Xcode \u4e2d\u62a5\u9519\uff1a<\/p>\n<pre>\nerror: cannot assign value of type 'String' to type 'Int'\nvarA = \"This is hello\"\n<\/pre>\n<p>\u610f\u601d\u4e3a\u4e0d\u80fd\u5c06 &#8216;String&#8217; \u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9 &#8216;Int&#8217; \u53d8\u91cf\u3002<\/p>\n<hr>\n<h2>\u7c7b\u578b\u63a8\u65ad<\/h2>\n<p>\u5f53\u4f60\u8981\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u65f6\uff0c\u7c7b\u578b\u68c0\u67e5\u53ef\u4ee5\u5e2e\u4f60\u907f\u514d\u9519\u8bef\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u662f\u8bf4\u4f60\u6bcf\u6b21\u58f0\u660e\u5e38\u91cf\u548c\u53d8\u91cf\u7684\u65f6\u5019\u90fd\u9700\u8981\u663e\u5f0f\u6307\u5b9a\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6ca1\u6709\u663e\u5f0f\u6307\u5b9a\u7c7b\u578b\uff0cSwift \u4f1a\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\uff08type inference\uff09\u6765\u9009\u62e9\u5408\u9002\u7684\u7c7b\u578b\u3002<\/p>\n<p>\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7ed9\u4e00\u4e2a\u65b0\u5e38\u91cf\u8d4b\u503c42\u5e76\u4e14\u6ca1\u6709\u6807\u660e\u7c7b\u578b\uff0cSwift \u53ef\u4ee5\u63a8\u65ad\u51fa\u5e38\u91cf\u7c7b\u578b\u662fInt\uff0c\u56e0\u4e3a\u4f60\u7ed9\u5b83\u8d4b\u7684\u521d\u59cb\u503c\u770b\u8d77\u6765\u50cf\u4e00\u4e2a\u6574\u6570\uff1a\n<\/p>\n<pre>\nlet meaningOfLife = 42\n\/\/ meaningOfLife \u4f1a\u88ab\u63a8\u6d4b\u4e3a Int \u7c7b\u578b\n<\/pre>\n<p>\n\u540c\u7406\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u7ed9\u6d6e\u70b9\u5b57\u9762\u91cf\u6807\u660e\u7c7b\u578b\uff0cSwift \u4f1a\u63a8\u65ad\u4f60\u60f3\u8981\u7684\u662fDouble\uff1a\n<\/p>\n<pre>\nlet pi = 3.14159\n\/\/ pi \u4f1a\u88ab\u63a8\u6d4b\u4e3a Double \u7c7b\u578b\n<\/pre>\n<p>\n\u5f53\u63a8\u65ad\u6d6e\u70b9\u6570\u7684\u7c7b\u578b\u65f6\uff0cSwift \u603b\u662f\u4f1a\u9009\u62e9Double\u800c\u4e0d\u662fFloat\u3002\n<\/p>\n<p>\n\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u540c\u65f6\u51fa\u73b0\u4e86\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u4f1a\u88ab\u63a8\u65ad\u4e3aDouble\u7c7b\u578b\uff1a\n<\/p>\n<pre>\nlet anotherPi = 3 + 0.14159\n\/\/ anotherPi \u4f1a\u88ab\u63a8\u6d4b\u4e3a Double \u7c7b\u578b<\/pre>\n<p>\n\u539f\u59cb\u503c3\u6ca1\u6709\u663e\u5f0f\u58f0\u660e\u7c7b\u578b\uff0c\u800c\u8868\u8fbe\u5f0f\u4e2d\u51fa\u73b0\u4e86\u4e00\u4e2a\u6d6e\u70b9\u5b57\u9762\u91cf\uff0c\u6240\u4ee5\u8868\u8fbe\u5f0f\u4f1a\u88ab\u63a8\u65ad\u4e3aDouble\u7c7b\u578b\u3002<\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\n\/\/ varA \u4f1a\u88ab\u63a8\u6d4b\u4e3a Int \u7c7b\u578b \nvar varA = 42\nprint(varA)\n\n\/\/ varB \u4f1a\u88ab\u63a8\u6d4b\u4e3a Double \u7c7b\u578b  \nvar varB = 3.14159\nprint(varB)\n\n\/\/ varC \u4e5f\u4f1a\u88ab\u63a8\u6d4b\u4e3a Double \u7c7b\u578b   \nvar varC = 3 + 0.14159\nprint(varC)\n<\/pre>\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n42\n3.14159\n3.14159\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u6570\u636e\u7c7b\u578b \u5728\u6211\u4eec\u4f7f\u7528\u4efb\u4f55\u7a0b\u5e8f\u8bed\u8a00\u7f16\u7a0b\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u5404\u79cd\u6570\u636e\u7c7b\u578b\u6765\u5b58\u50a8\u4e0d\u540c\u7684\u4fe1\u606f\u3002&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=509\">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-509","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\/509","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=509"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}