{"id":534,"date":"2018-02-23T23:39:03","date_gmt":"2018-02-23T15:39:03","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e9%97%ad%e5%8c%85\/"},"modified":"2018-02-23T23:39:03","modified_gmt":"2018-02-23T15:39:03","slug":"swift-%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=534","title":{"rendered":"Swift \u95ed\u5305"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u95ed\u5305<\/h1>\n<p>\u95ed\u5305(Closures)\u662f\u81ea\u5305\u542b\u7684\u529f\u80fd\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6216\u8005\u7528\u6765\u4f5c\u4e3a\u53c2\u6570\u4f20\u503c\u3002<\/p>\n<p>Swift \u4e2d\u7684\u95ed\u5305\u4e0e C \u548c Objective-C \u4e2d\u7684\u4ee3\u7801\u5757\uff08blocks\uff09\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 \u533f\u540d\u51fd\u6570\u6bd4\u8f83\u76f8\u4f3c\u3002<\/p>\n<p>\u5168\u5c40\u51fd\u6570\u548c\u5d4c\u5957\u51fd\u6570\u5176\u5b9e\u5c31\u662f\u7279\u6b8a\u7684\u95ed\u5305\u3002 <\/p>\n<p>\u95ed\u5305\u7684\u5f62\u5f0f\u6709\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<td>\u5168\u5c40\u51fd\u6570<\/td>\n<td>\u5d4c\u5957\u51fd\u6570<\/td>\n<td>\u95ed\u5305\u8868\u8fbe\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>\u6709\u540d\u5b57\u4f46\u4e0d\u80fd\u6355\u83b7\u4efb\u4f55\u503c\u3002 <\/td>\n<td>\u6709\u540d\u5b57\uff0c\u4e5f\u80fd\u6355\u83b7\u5c01\u95ed\u51fd\u6570\u5185\u7684\u503c\u3002 <\/td>\n<td>\u65e0\u540d\u95ed\u5305\uff0c\u4f7f\u7528\u8f7b\u91cf\u7ea7\u8bed\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0a\u4e0b\u6587\u73af\u5883\u6355\u83b7\u503c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Swift\u4e2d\u7684\u95ed\u5305\u6709\u5f88\u591a\u4f18\u5316\u7684\u5730\u65b9: <\/p>\n<ol>\n<li>\n\u6839\u636e\u4e0a\u4e0b\u6587\u63a8\u65ad\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b<\/li>\n<li>\n\u4ece\u5355\u884c\u8868\u8fbe\u5f0f\u95ed\u5305\u4e2d\u9690\u5f0f\u8fd4\u56de\uff08\u4e5f\u5c31\u662f\u95ed\u5305\u4f53\u53ea\u6709\u4e00\u884c\u4ee3\u7801\uff0c\u53ef\u4ee5\u7701\u7565return\uff09<\/li>\n<li>\n\u53ef\u4ee5\u4f7f\u7528\u7b80\u5316\u53c2\u6570\u540d\uff0c\u5982$0, $1(\u4ece0\u5f00\u59cb\uff0c\u8868\u793a\u7b2ci\u4e2a\u53c2\u6570&#8230;)<\/li>\n<li>\n\u63d0\u4f9b\u4e86\u5c3e\u968f\u95ed\u5305\u8bed\u6cd5(Trailing closure syntax)<\/li>\n<h3>\u8bed\u6cd5<\/h3>\n<p>\n\u4ee5\u4e0b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u63a5\u6536\u53c2\u6570\u5e76\u8fd4\u56de\u6307\u5b9a\u7c7b\u578b\u7684\u95ed\u5305\u8bed\u6cd5\uff1a<\/p>\n<pre>\n{(parameters) -&gt; return type in\n   statements\n}\n<\/pre>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nlet studname = { print(\"Swift \u95ed\u5305\u5b9e\u4f8b\u3002\") }\nstudname()\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nSwift \u95ed\u5305\u5b9e\u4f8b\u3002\n<\/pre>\n<p>\u4ee5\u4e0b\u95ed\u5305\u5f62\u5f0f\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5e03\u5c14\u503c\uff1a<\/p>\n<pre>\n{(Int, Int) -&gt; Bool in\n   Statement1\n   Statement 2\n    ---\n   Statement n\n}\n<\/pre>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nlet divide = {(val1: Int, val2: Int) -&gt; Int in \n   return val1 \/ val2 \n}\nlet result = divide(200, 20)\nprint (result)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n10\n<\/pre>\n<hr>\n<h2>\u95ed\u5305\u8868\u8fbe\u5f0f<\/h2>\n<p>\u95ed\u5305\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5229\u7528\u7b80\u6d01\u8bed\u6cd5\u6784\u5efa\u5185\u8054\u95ed\u5305\u7684\u65b9\u5f0f\u3002 \u95ed\u5305\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8bed\u6cd5\u4f18\u5316\uff0c\u4f7f\u5f97\u64b0\u5199\u95ed\u5305\u53d8\u5f97\u7b80\u5355\u660e\u4e86\u3002\n<\/p>\n<hr>\n<h3>sorted \u65b9\u6cd5<\/h3>\n<p>Swift \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u540d\u4e3a <strong>sorted(by:)<\/strong> \u7684\u65b9\u6cd5\uff0c\u4f1a\u6839\u636e\u60a8\u63d0\u4f9b\u7684\u7528\u4e8e\u6392\u5e8f\u7684\u95ed\u5305\u51fd\u6570\u5c06\u5df2\u77e5\u7c7b\u578b\u6570\u7ec4\u4e2d\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u3002 <\/p>\n<p>\u6392\u5e8f\u5b8c\u6210\u540e\uff0csorted(by:) \u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e0e\u539f\u6570\u7ec4\u5927\u5c0f\u76f8\u540c\uff0c\u5305\u542b\u540c\u7c7b\u578b\u5143\u7d20\u4e14\u5143\u7d20\u5df2\u6b63\u786e\u6392\u5e8f\u7684\u65b0\u6570\u7ec4\u3002\u539f\u6570\u7ec4\u4e0d\u4f1a\u88ab sorted(by:) \u65b9\u6cd5\u4fee\u6539\u3002\n<\/p>\n<p>sorted(by:)\u65b9\u6cd5\u9700\u8981\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n<\/ol>\n<ul>\n<li>\u5df2\u77e5\u7c7b\u578b\u7684\u6570\u7ec4<\/li>\n<li>\u95ed\u5305\u51fd\u6570\uff0c\u8be5\u95ed\u5305\u51fd\u6570\u9700\u8981\u4f20\u5165\u4e0e\u6570\u7ec4\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u7684\u4e24\u4e2a\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u7c7b\u578b\u503c\u6765\u8868\u660e\u5f53\u6392\u5e8f\u7ed3\u675f\u540e\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u6392\u5728\u7b2c\u4e8c\u4e2a\u53c2\u6570\u524d\u9762\u8fd8\u662f\u540e\u9762\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u503c\u51fa\u73b0\u5728\u7b2c\u4e8c\u4e2a\u53c2\u6570\u503c\u524d\u9762\uff0c\u6392\u5e8f\u95ed\u5305\u51fd\u6570\u9700\u8981\u8fd4\u56de <b>true<\/b>\uff0c\u53cd\u4e4b\u8fd4\u56de <b>false<\/b>\u3002<\/li>\n<\/ul>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nlet names = [\"AT\", \"AE\", \"D\", \"S\", \"BE\"]\n\n\/\/ \u4f7f\u7528\u666e\u901a\u51fd\u6570(\u6216\u5185\u5d4c\u51fd\u6570)\u63d0\u4f9b\u6392\u5e8f\u529f\u80fd,\u95ed\u5305\u51fd\u6570\u7c7b\u578b\u9700\u4e3a(String, String) -&gt; Bool\u3002\nfunc backwards(s1: String, s2: String) -&gt; Bool {\n    return s1 &gt; s2\n}\nvar reversed = names.sorted(by: backwards)\n\nprint(reversed)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n[\"S\", \"D\", \"BE\", \"AT\", \"AE\"]\n<\/pre>\n<p>\n\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32 (s1) \u5927\u4e8e\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32 (s2)\uff0cbackwards\u51fd\u6570\u8fd4\u56detrue\uff0c\u8868\u793a\u5728\u65b0\u7684\u6570\u7ec4\u4e2ds1\u5e94\u8be5\u51fa\u73b0\u5728s2\u524d\u3002 \u5bf9\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6765\u8bf4\uff0c&#8221;\u5927\u4e8e&#8221; \u8868\u793a &#8220;\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u8f83\u665a\u51fa\u73b0&#8221;\u3002 \u8fd9\u610f\u5473\u7740\u5b57\u6bcd&#8221;B&#8221;\u5927\u4e8e\u5b57\u6bcd&#8221;A&#8221;\uff0c\u5b57\u7b26\u4e32&#8221;S&#8221;\u5927\u4e8e\u5b57\u7b26\u4e32&#8221;D&#8221;\u3002 \u5176\u5c06\u8fdb\u884c\u5b57\u6bcd\u9006\u5e8f\u6392\u5e8f\uff0c&#8221;AT&#8221;\u5c06\u4f1a\u6392\u5728&#8221;AE&#8221;\u4e4b\u524d\u3002\n<\/p>\n<hr>\n<h2>\u53c2\u6570\u540d\u79f0\u7f29\u5199<\/h2>\n<p>Swift \u81ea\u52a8\u4e3a\u5185\u8054\u51fd\u6570\u63d0\u4f9b\u4e86\u53c2\u6570\u540d\u79f0\u7f29\u5199\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7$0,$1,$2\u6765\u987a\u5e8f\u8c03\u7528\u95ed\u5305\u7684\u53c2\u6570\u3002<\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nlet names = [\"AT\", \"AE\", \"D\", \"S\", \"BE\"]\n\nvar reversed = names.sorted( by: { $0 &gt; $1 } )\nprint(reversed)\n<\/pre>\n<p>$0\u548c$1\u8868\u793a\u95ed\u5305\u4e2d\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2aString\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n[\"S\", \"D\", \"BE\", \"AT\", \"AE\"]\n<\/pre>\n<p>\u5982\u679c\u4f60\u5728\u95ed\u5305\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u53c2\u6570\u540d\u79f0\u7f29\u5199, \u60a8\u53ef\u4ee5\u5728\u95ed\u5305\u53c2\u6570\u5217\u8868\u4e2d\u7701\u7565\u5bf9\u5176\u5b9a\u4e49, \u5e76\u4e14\u5bf9\u5e94\u53c2\u6570\u540d\u79f0\u7f29\u5199\u7684\u7c7b\u578b\u4f1a\u901a\u8fc7\u51fd\u6570\u7c7b\u578b\u8fdb\u884c\u63a8\u65ad\u3002in \u5173\u952e\u5b57\u540c\u6837\u4e5f\u53ef\u4ee5\u88ab\u7701\u7565.<\/p>\n<hr>\n<h2>\u8fd0\u7b97\u7b26\u51fd\u6570<\/h2>\n<p>\u5b9e\u9645\u4e0a\u8fd8\u6709\u4e00\u79cd\u66f4\u7b80\u77ed\u7684\u65b9\u5f0f\u6765\u64b0\u5199\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u95ed\u5305\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\nSwift \u7684<code>String<\/code>\u7c7b\u578b\u5b9a\u4e49\u4e86\u5173\u4e8e\u5927\u4e8e\u53f7 (<code>&gt;<\/code>) \u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\uff0c\u5176\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a<code>String<\/code>\u7c7b\u578b\u7684\u53c2\u6570\u5e76\u8fd4\u56de<code>Bool<\/code>\u7c7b\u578b\u7684\u503c\u3002<br \/>\n\u800c\u8fd9\u6b63\u597d\u4e0e<code>sort(_:)<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u9700\u8981\u7684\u51fd\u6570\u7c7b\u578b\u76f8\u7b26\u5408\u3002<br \/>\n\u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f20\u9012\u4e00\u4e2a\u5927\u4e8e\u53f7\uff0cSwift\u53ef\u4ee5\u81ea\u52a8\u63a8\u65ad\u51fa\u60a8\u60f3\u4f7f\u7528\u5927\u4e8e\u53f7\u7684\u5b57\u7b26\u4e32\u51fd\u6570\u5b9e\u73b0\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet names = [\"AT\", \"AE\", \"D\", \"S\", \"BE\"]\n\nvar reversed = names.sorted(by: &gt;)\nprint(reversed)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n[\"S\", \"D\", \"BE\", \"AT\", \"AE\"]\n<\/pre>\n<hr>\n<h2>\u5c3e\u968f\u95ed\u5305<\/h2>\n<p>\u5c3e\u968f\u95ed\u5305\u662f\u4e00\u4e2a\u4e66\u5199\u5728\u51fd\u6570\u62ec\u53f7\u4e4b\u540e\u7684\u95ed\u5305\u8868\u8fbe\u5f0f\uff0c\u51fd\u6570\u652f\u6301\u5c06\u5176\u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u8c03\u7528\u3002<\/p>\n<pre>\nfunc someFunctionThatTakesAClosure(closure: () -&gt; Void) {\n    \/\/ \u51fd\u6570\u4f53\u90e8\u5206\n}\n\n\/\/ \u4ee5\u4e0b\u662f\u4e0d\u4f7f\u7528\u5c3e\u968f\u95ed\u5305\u8fdb\u884c\u51fd\u6570\u8c03\u7528\nsomeFunctionThatTakesAClosure({\n    \/\/ \u95ed\u5305\u4e3b\u4f53\u90e8\u5206\n})\n\n\/\/ \u4ee5\u4e0b\u662f\u4f7f\u7528\u5c3e\u968f\u95ed\u5305\u8fdb\u884c\u51fd\u6570\u8c03\u7528\nsomeFunctionThatTakesAClosure() {\n  \/\/ \u95ed\u5305\u4e3b\u4f53\u90e8\u5206\n}\n<\/pre>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nlet names = [\"AT\", \"AE\", \"D\", \"S\", \"BE\"]\n\n\/\/\u5c3e\u968f\u95ed\u5305\nvar reversed = names.sorted() { $0 &gt; $1 }\nprint(reversed)\n<\/pre>\n<p>sort() \u540e\u7684 { $0 &gt; $1} \u4e3a\u5c3e\u968f\u95ed\u5305\u3002<\/p>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n[\"S\", \"D\", \"BE\", \"AT\", \"AE\"]\n<\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a<br \/>\n\u5982\u679c\u51fd\u6570\u53ea\u9700\u8981\u95ed\u5305\u8868\u8fbe\u5f0f\u4e00\u4e2a\u53c2\u6570\uff0c\u5f53\u60a8\u4f7f\u7528\u5c3e\u968f\u95ed\u5305\u65f6\uff0c\u60a8\u751a\u81f3\u53ef\u4ee5\u628a<code>()<\/code>\u7701\u7565\u6389\u3002<\/p>\n<pre>\nreversed = names.sorted { $0 &gt; $1 }\n<\/pre>\n<\/blockquote>\n<hr>\n<h2>\u6355\u83b7\u503c<\/h2>\n<p>\u95ed\u5305\u53ef\u4ee5\u5728\u5176\u5b9a\u4e49\u7684\u4e0a\u4e0b\u6587\u4e2d\u6355\u83b7\u5e38\u91cf\u6216\u53d8\u91cf\u3002<\/p>\n<p> \u5373\u4f7f\u5b9a\u4e49\u8fd9\u4e9b\u5e38\u91cf\u548c\u53d8\u91cf\u7684\u539f\u57df\u5df2\u7ecf\u4e0d\u5b58\u5728\uff0c\u95ed\u5305\u4ecd\u7136\u53ef\u4ee5\u5728\u95ed\u5305\u51fd\u6570\u4f53\u5185\u5f15\u7528\u548c\u4fee\u6539\u8fd9\u4e9b\u503c\u3002<\/p>\n<p>Swift\u6700\u7b80\u5355\u7684\u95ed\u5305\u5f62\u5f0f\u662f\u5d4c\u5957\u51fd\u6570\uff0c\u4e5f\u5c31\u662f\u5b9a\u4e49\u5728\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\u4f53\u5185\u7684\u51fd\u6570\u3002 <\/p>\n<p>\u5d4c\u5957\u51fd\u6570\u53ef\u4ee5\u6355\u83b7\u5176\u5916\u90e8\u51fd\u6570\u6240\u6709\u7684\u53c2\u6570\u4ee5\u53ca\u5b9a\u4e49\u7684\u5e38\u91cf\u548c\u53d8\u91cf\u3002<\/p>\n<p>\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>\nfunc makeIncrementor(forIncrement amount: Int) -&gt; () -&gt; Int {\n    var runningTotal = 0\n    func incrementor() -&gt; Int {\n        runningTotal += amount\n        return runningTotal\n    }\n    return incrementor\n}\n<\/pre>\n<p>\u4e00\u4e2a\u51fd\u6570makeIncrementor \uff0c\u5b83\u6709\u4e00\u4e2aInt\u578b\u7684\u53c2\u6570amout, \u5e76\u4e14\u5b83\u6709\u4e00\u4e2a\u5916\u90e8\u53c2\u6570\u540d\u5b57forIncremet\uff0c\u610f\u5473\u7740\u4f60\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u4f7f\u7528\u8fd9\u4e2a\u5916\u90e8\u540d\u5b57\u3002\u8fd4\u56de\u503c\u662f\u4e00\u4e2a<code>()-&gt; Int<\/code>\u7684\u51fd\u6570\u3002<\/p>\n<p>\u51fd\u6570\u9898\u5185\uff0c\u58f0\u660e\u4e86\u53d8\u91cfrunningTotal \u548c\u4e00\u4e2a\u51fd\u6570incrementor\u3002<\/p>\n<p data-anchor-id=\"8xrc\">incrementor\u51fd\u6570\u5e76\u6ca1\u6709\u83b7\u53d6\u4efb\u4f55\u53c2\u6570\uff0c\u4f46\u662f\u5728\u51fd\u6570\u4f53\u5185\u8bbf\u95ee\u4e86runningTotal\u548camount\u53d8\u91cf\u3002\u8fd9\u662f\u56e0\u4e3a\u5176\u901a\u8fc7\u6355\u83b7\u5728\u5305\u542b\u5b83\u7684\u51fd\u6570\u4f53\u5185\u5df2\u7ecf\u5b58\u5728\u7684runningTotal\u548camount\u53d8\u91cf\u800c\u5b9e\u73b0\u3002<\/p>\n<p>\u7531\u4e8e\u6ca1\u6709\u4fee\u6539amount\u53d8\u91cf\uff0cincrementor\u5b9e\u9645\u4e0a\u6355\u83b7\u5e76\u5b58\u50a8\u4e86\u8be5\u53d8\u91cf\u7684\u4e00\u4e2a\u526f\u672c\uff0c\u800c\u8be5\u526f\u672c\u968f\u7740incrementor\u4e00\u540c\u88ab\u5b58\u50a8\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\u4f1a\u7d2f\u52a0\uff1a<\/p>\n<pre>\nimport Cocoa\n\nfunc makeIncrementor(forIncrement amount: Int) -&gt; () -&gt; Int {\n    var runningTotal = 0\n    func incrementor() -&gt; Int {\n        runningTotal += amount\n        return runningTotal\n    }\n    return incrementor\n}\n\nlet incrementByTen = makeIncrementor(forIncrement: 10)\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a10\nprint(incrementByTen())\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a20\nprint(incrementByTen())\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a30\nprint(incrementByTen())\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n10\n20\n30\n<\/pre>\n<hr>\n<h2>\u95ed\u5305\u662f\u5f15\u7528\u7c7b\u578b<\/h2>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cincrementByTen\u662f\u5e38\u91cf\uff0c\u4f46\u662f\u8fd9\u4e9b\u5e38\u91cf\u6307\u5411\u7684\u95ed\u5305\u4ecd\u7136\u53ef\u4ee5\u589e\u52a0\u5176\u6355\u83b7\u7684\u53d8\u91cf\u503c\u3002<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u51fd\u6570\u548c\u95ed\u5305\u90fd\u662f\u5f15\u7528\u7c7b\u578b\u3002<\/p>\n<p>\u65e0\u8bba\u60a8\u5c06\u51fd\u6570\/\u95ed\u5305\u8d4b\u503c\u7ed9\u4e00\u4e2a\u5e38\u91cf\u8fd8\u662f\u53d8\u91cf\uff0c\u60a8\u5b9e\u9645\u4e0a\u90fd\u662f\u5c06\u5e38\u91cf\/\u53d8\u91cf\u7684\u503c\u8bbe\u7f6e\u4e3a\u5bf9\u5e94\u51fd\u6570\/\u95ed\u5305\u7684\u5f15\u7528\u3002 \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cincrementByTen\u6307\u5411\u95ed\u5305\u7684\u5f15\u7528\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u800c\u5e76\u975e\u95ed\u5305\u5185\u5bb9\u672c\u8eab\u3002<\/p>\n<p>\u8fd9\u4e5f\u610f\u5473\u7740\u5982\u679c\u60a8\u5c06\u95ed\u5305\u8d4b\u503c\u7ed9\u4e86\u4e24\u4e2a\u4e0d\u540c\u7684\u5e38\u91cf\/\u53d8\u91cf\uff0c\u4e24\u4e2a\u503c\u90fd\u4f1a\u6307\u5411\u540c\u4e00\u4e2a\u95ed\u5305\uff1a<\/p>\n<pre>\nimport Cocoa\n\nfunc makeIncrementor(forIncrement amount: Int) -&gt; () -&gt; Int {\n    var runningTotal = 0\n    func incrementor() -&gt; Int {\n        runningTotal += amount\n        return runningTotal\n    }\n    return incrementor\n}\n\nlet incrementByTen = makeIncrementor(forIncrement: 10)\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a10\nincrementByTen()\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a20\nincrementByTen()\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a30\nincrementByTen()\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e3a40\nincrementByTen()\n\nlet alsoIncrementByTen = incrementByTen\n\n\/\/ \u8fd4\u56de\u7684\u503c\u4e5f\u4e3a50\nprint(alsoIncrementByTen())\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>50<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u95ed\u5305 \u95ed\u5305(Closures)\u662f\u81ea\u5305\u542b\u7684\u529f\u80fd\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6216\u8005\u7528\u6765\u4f5c&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=534\">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-534","post","type-post","status-publish","format-standard","hentry","category-mobile","category-swift","tag-ios","tag-swift","zoom-theme-has-thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift \u95ed\u5305 - \u8c31\u5b50\u68cb - Puzi7<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.puzi7.net\/?p=534\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift \u95ed\u5305 - \u8c31\u5b50\u68cb - Puzi7\" \/>\n<meta property=\"og:description\" content=\"Swift \u95ed\u5305 \u95ed\u5305(Closures)\u662f\u81ea\u5305\u542b\u7684\u529f\u80fd\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6216\u8005\u7528\u6765\u4f5c&hellip;Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.puzi7.net\/?p=534\" \/>\n<meta property=\"og:site_name\" content=\"\u8c31\u5b50\u68cb - Puzi7\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-23T15:39:03+00:00\" \/>\n<meta name=\"author\" content=\"\u77f3\u8863\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u77f3\u8863\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534\"},\"author\":{\"name\":\"\u77f3\u8863\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#\\\/schema\\\/person\\\/87dd4941c8ec260c1350b7f41980a190\"},\"headline\":\"Swift \u95ed\u5305\",\"datePublished\":\"2018-02-23T15:39:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534\"},\"wordCount\":61,\"commentCount\":0,\"keywords\":[\"iOS\",\"Swift\"],\"articleSection\":[\"Mobile\",\"Swift\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.puzi7.net\\\/?p=534#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534\",\"url\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534\",\"name\":\"Swift \u95ed\u5305 - \u8c31\u5b50\u68cb - Puzi7\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#website\"},\"datePublished\":\"2018-02-23T15:39:03+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#\\\/schema\\\/person\\\/87dd4941c8ec260c1350b7f41980a190\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.puzi7.net\\\/?p=534\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/?p=534#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6280\u672f\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/?cat=9\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Mobile\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/?cat=26\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Swift\",\"item\":\"https:\\\/\\\/www.puzi7.net\\\/?cat=28\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Swift \u95ed\u5305\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#website\",\"url\":\"https:\\\/\\\/www.puzi7.net\\\/\",\"name\":\"\u8c31\u5b50\u68cb - Puzi7\",\"description\":\"\u5206\u4eab\uff0c\u6210\u957f\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.puzi7.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.puzi7.net\\\/#\\\/schema\\\/person\\\/87dd4941c8ec260c1350b7f41980a190\",\"name\":\"\u77f3\u8863\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g\",\"caption\":\"\u77f3\u8863\"},\"sameAs\":[\"https:\\\/\\\/www.puzi7.net\"],\"url\":\"https:\\\/\\\/www.puzi7.net\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift \u95ed\u5305 - \u8c31\u5b50\u68cb - Puzi7","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.puzi7.net\/?p=534","og_locale":"zh_CN","og_type":"article","og_title":"Swift \u95ed\u5305 - \u8c31\u5b50\u68cb - Puzi7","og_description":"Swift \u95ed\u5305 \u95ed\u5305(Closures)\u662f\u81ea\u5305\u542b\u7684\u529f\u80fd\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6216\u8005\u7528\u6765\u4f5c&hellip;Read More","og_url":"https:\/\/www.puzi7.net\/?p=534","og_site_name":"\u8c31\u5b50\u68cb - Puzi7","article_published_time":"2018-02-23T15:39:03+00:00","author":"\u77f3\u8863","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u77f3\u8863","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.puzi7.net\/?p=534#article","isPartOf":{"@id":"https:\/\/www.puzi7.net\/?p=534"},"author":{"name":"\u77f3\u8863","@id":"https:\/\/www.puzi7.net\/#\/schema\/person\/87dd4941c8ec260c1350b7f41980a190"},"headline":"Swift \u95ed\u5305","datePublished":"2018-02-23T15:39:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.puzi7.net\/?p=534"},"wordCount":61,"commentCount":0,"keywords":["iOS","Swift"],"articleSection":["Mobile","Swift"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.puzi7.net\/?p=534#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.puzi7.net\/?p=534","url":"https:\/\/www.puzi7.net\/?p=534","name":"Swift \u95ed\u5305 - \u8c31\u5b50\u68cb - Puzi7","isPartOf":{"@id":"https:\/\/www.puzi7.net\/#website"},"datePublished":"2018-02-23T15:39:03+00:00","author":{"@id":"https:\/\/www.puzi7.net\/#\/schema\/person\/87dd4941c8ec260c1350b7f41980a190"},"breadcrumb":{"@id":"https:\/\/www.puzi7.net\/?p=534#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.puzi7.net\/?p=534"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.puzi7.net\/?p=534#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.puzi7.net\/"},{"@type":"ListItem","position":2,"name":"\u6280\u672f","item":"https:\/\/www.puzi7.net\/?cat=9"},{"@type":"ListItem","position":3,"name":"Mobile","item":"https:\/\/www.puzi7.net\/?cat=26"},{"@type":"ListItem","position":4,"name":"Swift","item":"https:\/\/www.puzi7.net\/?cat=28"},{"@type":"ListItem","position":5,"name":"Swift \u95ed\u5305"}]},{"@type":"WebSite","@id":"https:\/\/www.puzi7.net\/#website","url":"https:\/\/www.puzi7.net\/","name":"\u8c31\u5b50\u68cb - Puzi7","description":"\u5206\u4eab\uff0c\u6210\u957f","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.puzi7.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.puzi7.net\/#\/schema\/person\/87dd4941c8ec260c1350b7f41980a190","name":"\u77f3\u8863","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/secure.gravatar.com\/avatar\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c52c3a64b447af78e58cfa1c73ddd53952049afcf934cd702d71eee91f9907bd?s=96&d=mm&r=g","caption":"\u77f3\u8863"},"sameAs":["https:\/\/www.puzi7.net"],"url":"https:\/\/www.puzi7.net\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/534","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=534"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}