{"id":7390,"date":"2022-09-13T22:57:10","date_gmt":"2022-09-13T14:57:10","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7390"},"modified":"2022-09-14T08:01:56","modified_gmt":"2022-09-14T00:01:56","slug":"swift%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7390","title":{"rendered":"Swift\u95ed\u5305"},"content":{"rendered":"\n<p><strong>\u95ed\u5305\u7684\u6982\u5ff5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e00\u95e8\u8ba1\u7b97\u673a\u8bed\u8a00\u5982\u679c\u8981\u652f\u6301\u95ed\u5305\uff0c\u5fc5\u987b\u8981\u6709\u4e24\u4e2a\u524d\u63d0\uff1a<strong>\u652f\u6301\u51fd\u6570\u7c7b\u578b<\/strong>\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ef\u4ee5\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u8fdb\u884c\u4f20\u9012\uff0c\u6216\u8005\u80fd\u591f\u5c06\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\uff1b<strong>\u652f\u6301\u51fd\u6570\u5d4c\u5957<\/strong>\u3002\u95ed\u5305\u662f\u4e00\u79cd\u81ea\u5305\u542b\u7684\u533f\u540d\u51fd\u6570\u4ee3\u7801\u5757\uff0c\u5b83\u53ef\u4ee5\u4f5c\u4e3a<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E8%A1%A8%E8%BE%BE%E5%BC%8F&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u8868\u8fbe\u5f0f<\/a>\u3001\u51fd\u6570\u53c2\u6570\u6216\u8005\u51fd\u6570\u8fd4\u56de\u503c\u3002\u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u8fd0\u7b97\u7ed3\u679c\u662f\u4e00\u79cd\u51fd\u6570\u7c7b\u578b\u3002<strong>\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7406\u89e3\u6210\u201c\u5b9a\u4e49\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570\u201c<\/strong>\u3002<\/p>\n\n\n\n<p>\u95ed\u5305\uff1a\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570,\u53ef\u4ee5\u7406\u89e3\u6210\u5b9a\u4e49\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570.<br>\u7b80\u5355\u7684\u8bf4\u5b83\u5c31\u662f\u4e00\u4e2a\u4ee3\u7801\u5757,\u7528{}\u5305\u8d77\u6765,\u4ed6\u53ef\u4ee5\u7528\u5728\u5176\u4ed6\u51fd\u6570\u7684\u5185\u90e8,\u5c06\u5176\u4ed6\u51fd\u6570\u7684\u53d8\u91cf\u4f5c\u4e3a\u4ee3\u7801\u5757\u7684\u53c2\u6570\u4f20\u5165\u4ee3\u7801\u5757\u4e2d.\u5728Swift\u4e2d\u591a\u7528\u4e8e\u56de\u8c03<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570\u89e3\u91ca\u8bf4\u660e<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function f1(){\n \n\u3000\u3000\u3000\u3000var n=999;\n \n\u3000\u3000\u3000\u3000function f2(){\n\u3000\u3000\u3000\u3000\u3000\u3000alert(n);\n\u3000\u3000\u3000\u3000}\n \n\u3000\u3000\u3000\u3000return f2;\n \n\u3000\u3000}\n \n\u3000\u3000var result=f1();\n \n\u3000\u3000result(); \/\/ 999\n\n\/\/ \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570f2\u5c31\u88ab\u5305\u62ec\u5728\u51fd\u6570f1\u5185\u90e8\uff0c\u8fd9\u65f6f1\u5185\u90e8\u7684\u6240\u6709\u5c40\u90e8\u53d8\u91cf\uff0c\u5bf9f2\u90fd\u662f\u53ef\u89c1\u7684\u3002\u4f46\u662f\u53cd\u8fc7\u6765\u5c31\u4e0d\u884c\uff0cf2\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5bf9f1\u5c31\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u5b50\u5bf9\u8c61\u4f1a\u4e00\u7ea7\u4e00\u7ea7\u5730\u5411\u4e0a\u5bfb\u627e\u6240\u6709\u7236\u5bf9\u8c61\u7684\u53d8\u91cf\u3002\u6240\u4ee5\uff0c\u7236\u5bf9\u8c61\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5bf9\u5b50\u5bf9\u8c61\u90fd\u662f\u53ef\u89c1\u7684\uff0c\u53cd\u4e4b\u5219\u4e0d\u6210\u7acb\u3002\n\/\/ \u65e2\u7136f2\u53ef\u4ee5\u8bfb\u53d6f1\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u53ea\u8981\u628af2\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u4e0d\u5c31\u53ef\u4ee5\u5728f1\u5916\u90e8\u8bfb\u53d6\u5b83\u7684\u5185\u90e8\u53d8\u91cf\u4e86\u5417\uff01\n\/\/ f2\u51fd\u6570\uff0c\u5c31\u662f\u95ed\u5305\u3002\n\/\/ \u95ed\u5305\u53ef\u4ee5\u7528\u5728\u8bb8\u591a\u5730\u65b9\u3002\u5b83\u7684\u6700\u5927\u7528\u5904\u6709\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662f\u524d\u9762\u63d0\u5230\u7684\u53ef\u4ee5\u8bfb\u53d6\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff0c\u53e6\u4e00\u4e2a\u5c31\u662f\u8ba9\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u59cb\u7ec8\u4fdd\u6301\u5728\u5185\u5b58\u4e2d\uff08\u5ef6\u957f\u53d8\u91cf\u7684\u58f0\u660e\u5468\u671f\uff0c\u5e38\u9a7b\u5185\u5b58\uff09<\/pre>\n\n\n\n<p>\u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u6807\u51c6\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u6807\u51c6\u683c\u5f0f\n{(\u53c2\u6570\u5217\u8868) -> \u8fd4\u56de\u503c\u7c7b\u578b in\n    \u8bed\u53e5\u7ec4\n}\n \n\/\/ \u95ed\u5305\u5f62\u5f0f\n{(a: int, b: Int) -> Int in\n    return a + b\n}\n<\/pre>\n\n\n\n<p>\u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u53c2\u6570\u5217\u8868\u548c\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u5f62\u5f0f\u4e00\u6837\uff0c\u5176\u8fd4\u56de\u503c\u7c7b\u578b\u4e5f\u548c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u76f8\u4f3c\u3002\u95ed\u5305\u4e0e\u51fd\u6570\u6bd4\u8f83\u660e\u663e\u7684\u4e00\u4e2a\u5dee\u522b\u662f\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u540e\u9762\u591a\u4e86\u4e00\u4e2a\u5173\u952e\u5b57in\u3002<\/p>\n\n\n\n<p><em>\u95ed\u5305<\/em>\u662f\u81ea\u5305\u542b\u7684\u51fd\u6570\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u88ab\u4f20\u9012\u548c\u4f7f\u7528\u3002Swift \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\n\n\n<p>\u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u548c\u5b58\u50a8\u5176\u6240\u5728\u4e0a\u4e0b\u6587\u4e2d\u4efb\u610f\u5e38\u91cf\u548c\u53d8\u91cf\u7684\u5f15\u7528\u3002\u88ab\u79f0\u4e3a<em>\u5305\u88f9<\/em>\u5e38\u91cf\u548c\u53d8\u91cf\u3002 Swift \u4f1a\u4e3a\u4f60\u7ba1\u7406\u5728\u6355\u83b7\u8fc7\u7a0b\u4e2d\u6d89\u53ca\u5230\u7684\u6240\u6709\u5185\u5b58\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u4e3b\u8981\u4f18\u5316<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u5229\u7528\u4e0a\u4e0b\u6587\u63a8\u65ad\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b<\/li><li>\u9690\u5f0f\u8fd4\u56de\u5355\u8868\u8fbe\u5f0f\u95ed\u5305\uff0c\u5373\u5355\u8868\u8fbe\u5f0f\u95ed\u5305\u53ef\u4ee5\u7701\u7565&nbsp;<code>return<\/code>&nbsp;\u5173\u952e\u5b57<\/li><li>\u53c2\u6570\u540d\u79f0\u7f29\u5199<\/li><li>\u5c3e\u968f\u95ed\u5305\u8bed\u6cd5<\/li><\/ul>\n\n\n\n<p><strong>\u5229\u7528\u7c7b\u578b\u63a8\u65ad\u5bf9\u95ed\u5305\u8fdb\u884c\u7b80\u5316<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u2003\u2003\u5728Swift\u4e2d\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u4e0a\u4e0b\u6587\u7684\u73af\u5883\u63a8\u65ad\u51fa\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u6bd4\u5982\u8bf4\u50cf\u4e0a\u9762\u90a3\u4e2a\u95ed\u5305\u6807\u51c6\u5f62\u5f0f\uff0c\u5b8c\u5168\u53ef\u4ee5\u5229\u7528\u7f16\u8bd1\u5668\u7279\u6027\u5c06\u5176\u7b80\u5316\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u95ed\u5305\u5f62\u5f0f\n{(a, b) in\n    return a + b\n}\n \n\/\/ \u7701\u7565\u53c2\u6570\u5217\u8868\u5c0f\u62ec\u53f7\uff0c\u5e76\u4e14\u5c06\u5176\u5199\u6210\u4e00\u884c\n{a, b in return a + b}<\/pre>\n\n\n\n<p><strong>\u9690\u85cfreturn\u5173\u952e\u5b57<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u2003\u2003\u5982\u679c\u95ed\u5305\u5185\u90e8\u8bed\u53e5\u7ec4\u4e2d\u53ea\u6709\u4e00\u6761\u8bed\u53e5\uff0c\u6bd4\u5982\u8bf4\u50cf\u4e0a\u9762\u7684return a + b\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u8fd4\u56de\u8bed\u53e5\u4e2d\u7684return\u5173\u952e\u5b57\u7ed9\u7701\u7565\u6389\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u7701\u7565\u8fd4\u56de\u8bed\u53e5\u4e2d\u7684\u5173\u952e\u5b57return\n{a, b in a + b}<\/pre>\n\n\n\n<p>\u2003\u2003\u9700\u8981\u7279\u522b\u5f3a\u8c03\u4e00\u4e0b\uff0c\u53ea\u6709\u5f53\u5173\u952e\u5b57in\u540e\u9762\u7684\u8bed\u53e5\u7ec4\u53ea\u6709\u4e00\u6761\u8fd4\u56de\u8bed\u53e5\u65f6\uff0c\u4f60\u624d\u80fd\u5c06\u5173\u952e\u5b57return\u7ed9\u7701\u7565\u6389\u3002\u5982\u679c\u5173\u952e\u5b57in\u540e\u9762\u6709\u591a\u6761\u8bed\u53e5\uff0c\u90a3\u4e48\u5173\u952e\u5b57return\u5219\u4e0d\u80fd\u7701\u7565\u3002\u6bd4\u5982\u8bf4\uff0c\u50cf{a, b in var c = 0; a + b}\u8fd9\u79cd\u5173\u952e\u5b57in\u540e\u9762\u6709\u591a\u6761\u8bed\u53e5\u7684\u60c5\u51b5\uff0c\u7701\u7565return\u5c31\u662f\u9519\u8bef\u7684\u5199\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u7701\u7565\u53c2\u6570\u540d<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0a\u9762\u7684\u95ed\u5305\u5df2\u7ecf\u975e\u5e38\u7b80\u6d01\u4e86\uff0c\u4f46\u662f\u8fd8\u4e0d\u662f\u6700\u7b80\u6d01\u7684\u3002\u5176\u5b9e\u8fd8\u53ef\u4ee5\u518d\u6781\u7aef\u4e00\u70b9\uff0c\u5c06\u53c2\u6570\u540d\u4e5f\u7ed9\u7701\u7565\u6389\u3002\u6211\u4eec\u53ef\u4ee5\u7528$0\u3001$1\u3001$3\u2026$n\u6765\u6307\u5b9a\u95ed\u5305\u4e2d\u7684\u53c2\u6570\uff0c\u5176\u4e2d$0\u8868\u793a\u95ed\u5305\u53c2\u6570\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c$1\u8868\u793a\u95ed\u5305\u53c2\u6570\u5217\u8868\u4e2d\u7684\u7b2c2\u4e2a\u53c2\u6570\u2026\u4f9d\u6b64\u7c7b\u63a8\u3002\u9664\u4e86\u53c2\u6570\u5217\u8868\u53ef\u4ee5\u7701\u7565\u4e4b\u5916\uff0c\u5173\u952e\u5b57in\u4e5f\u53ef\u4ee5\u7701\u7565\uff0c\u4e0a\u9762\u7684\u95ed\u5305\u53ef\u4ee5\u7b80\u5199\u4e3a\uff1a<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{$0 + $1}\n<\/pre>\n\n\n\n<p><strong>\u4f7f\u7528\u95ed\u5305\u4f5c\u4e3a\u8fd4\u56de\u503c<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u2003\u2003\u95ed\u5305\u8868\u8fbe\u5f0f\u672c\u8d28\u4e0a\u662f\u51fd\u6570\u7c7b\u578b\uff0c\u51fd\u6570\u91cc\u9762\u6709\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u95ed\u5305\u91cc\u9762\u80af\u5b9a\u4e5f\u6709\u95ed\u5305\u8fd4\u56de\u503c\u3002\u6bd4\u5982\u8bf4\uff0c\u6211\u4eec\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u58f0\u660e\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\uff0c\u7528\u4e8e\u63a5\u6536\u95ed\u5305\u8fd4\u56de\u503c\nlet num1: Int = {(a: Int, b: Int) -> Int in\n                    return a + b\n                 }(10, 5)\nprint(num1)  \/\/ \u7ed3\u679c\u4e3a15\n \nlet num2: Int = {$0 - $1}(10, 5)\nprint(num2)  \/\/ \u7ed3\u679c\u4e3a5<\/pre>\n\n\n\n<p>\u2003\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cnum1\u4e3aInt\u7c7b\u578b\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u80fd\u76f4\u63a5\u5c06\u95ed\u5305\u8868\u8fbe\u5f0f\u8d4b\u503c\u7ed9\u5b83\u3002\u4f46\u662f\u8fd9\u4e2a\u95ed\u5305\u662f\u6709\u8fd4\u56de\u503c\u7684\uff0c\u800c\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u521a\u597d\u4e5f\u662fInt\u7c7b\u578b\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u5728\u95ed\u5305\u672b\u5c3e\u5927\u62ec\u53f7\u540e\u9762\u518d\u8ddf\u4e0a\u4e00\u5bf9\u5c0f\u62ec\u53f7\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5c0f\u62ec\u53f7\u4e3a\u95ed\u5305\u4f20\u9012\u53c2\u6570\u3002\u9700\u8981\u6ce8\u610f\uff0c\u8fd9\u79cd\u5199\u6cd5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u62e5\u6709\u5e7f\u6cdb\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u5bf9\u63a7\u4ef6\u8fdb\u884c\u61d2\u52a0\u8f7d\u65f6\uff0c\u7528\u5f97\u975e\u5e38\u7684\u591a\uff01<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>&nbsp; &nbsp; \u95ed\u5305\u548cblock\u7684\u5e94\u7528\u573a\u666f\u662f\u4e00\u6837\u7684.(\u5f53\u4f60\u89c9\u5f97\u8fd9\u79cd\u60c5\u51b5\u5728oc\u4e2d\u7528block\u80fd\u5b9e\u73b0\u7684,\u90a3\u4e48\u5728swift\u4e2d\u7528\u95ed\u5305\u4e5f\u80fd\u5b9e\u73b0)<\/p>\n\n\n\n<ul><li>\u5f02\u6b65\u6267\u884c\u5b8c\u6210\u56de\u8c03.<\/li><li>\u63a7\u5236\u5668\u95f4\u56de\u8c03<\/li><li>\u81ea\u5b9a\u4e49\u89c6\u56fe\u56de\u8c03<\/li><\/ul>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u4e09\u79cd\u6a21\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u95ed\u5305\u6709\u4e09\u79cd\u5f62\u5f0f,  \u8fd9\u91cc\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684,\u4e00\u5b9a\u8981\u53bb\u6267\u884c\u8c03\u7528\u95ed\u5305,\u5426\u5219\u91cc\u8fb9\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c<\/p>\n\n\n\n<ul><li>1\u6ca1\u6709\u53c2\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u95ed\u5305(\u6700\u7b80\u5355\u7684\u95ed\u5305)<\/li><li>2,\u5e26\u53c2\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u95ed\u5305<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-1024x432.png\" alt=\"\" class=\"wp-image-7391\" width=\"408\" height=\"172\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-1024x432.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-300x126.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-768x324.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-830x350.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-230x97.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-350x148.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72-480x202.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-72.png 1082w\" sizes=\"(max-width: 408px) 100vw, 408px\" \/><\/figure><\/div>\n\n\n\n<ul><li>3,\u5e26\u53c2\u6570\u5e26\u8fd4\u56de\u503c\u7684\u95ed\u5305<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-1024x286.png\" alt=\"\" class=\"wp-image-7392\" width=\"400\" height=\"111\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-1024x286.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-300x84.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-768x215.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-830x232.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-230x64.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-350x98.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73-480x134.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-73.png 1194w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><figcaption>\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f,\u8fd4\u56de\u503c\u9700\u8981\u8fdb\u884c\u64cd\u4f5c,\u4e0d\u7136\u4f1a\u62a5\u8b66\u544a.<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u7528<strong> in<\/strong> \u53bb\u533a\u5206\u53bb\u5206\u5272\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u5b9e\u73b0,\u8bf4\u901a\u4fd7\u70b9\u5c31\u662f\u5206\u5272 \u4f20\u53c2\u548c\u540e\u7eed\u64cd\u4f5c\u7684\u5206\u5272!!!<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u56de\u8c03<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u95ed\u5305\u56de\u8c03,\u4e5f\u5c31\u662f\u8bf4\u5c06\u95ed\u5305\u4ee5\u53c2\u6570\u7684\u5f62\u5f0f\u8fdb\u884c\u56de\u8c03.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-1024x832.png\" alt=\"\" class=\"wp-image-7393\" width=\"485\" height=\"394\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-1024x832.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-300x244.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-768x624.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-830x674.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-230x187.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-350x284.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74-480x390.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-74.png 1344w\" sizes=\"(max-width: 485px) 100vw, 485px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5c3e\u968f\u95ed\u5305<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5c3e\u968f\u95ed\u5305\u662f\u8c03\u7528\u8f83\u7b80\u6d01\u7684\u5199\u6cd5\uff0c\u95ed\u5305\u8868\u8fbe\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8fdb\u884c\u4f20\u9012\uff0c<strong>\u5b83\u8fd8\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u8fdb\u884c\u4f20\u9012<\/strong>\u3002\u4f46\u662f\uff0c\u5982\u679c\u95ed\u5305\u8868\u8fbe\u5f0f\u5f88\u957f\uff0c\u5c31\u4f1a\u5bfc\u81f4\u6574\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u4e5f\u975e\u5e38\u7684\u957f\uff0c\u8fd9\u6837\u5f88\u5bb9\u6613\u5f71\u54cd\u7a0b\u5e8f\u7684\u9605\u8bfb\u6027\u3002\u4e0d\u8fc7\uff0c\u597d\u5728Swift\u51fd\u6570\u652f\u6301\u5c3e\u968f\u95ed\u5305\u3002\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\uff0c\u7136\u540e\u518d\u6765\u89e3\u91ca\u4ec0\u4e48\u662f\u5c3e\u968f\u95ed\u5305<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5c3e\u968f\u95ed\u5305\nfunc calculate(opt: String, closureP: (Int, Int) -> Int) {\n \n    switch opt {\n    case \"+\":\n        print(\"10 + 5 = \\(closureP(10, 5))\")\n    default:\n        print(\"10 - 5 = \\(closureP(10, 5))\")\n    }\n}\n \n\/\/ \u51fd\u6570\u8c03\u75281\uff1a\u8c03\u7528\u7684\u65f6\u5019\u5c06\u95ed\u5305\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\ncalculate(opt: \"+\", closureP: {(a: Int, b: Int) -> Int in return a + b})\n \n\/\/ \u51fd\u6570\u8c03\u75282\uff1a\u8c03\u7528\u7684\u65f6\u5019\u5c06\u95ed\u5305\u8868\u8fbe\u5f0f\u79fb\u5230\u51fd\u6570\u5c0f\u62ec\u53f7\u540e\u9762\ncalculate(opt: \"+\") { (a: Int, b: Int) -> Int in\n    return a + b\n}\n \n\/\/ \u51fd\u6570\u8c03\u75283\uff1a\u5bf9\u51fd\u6570\u8c03\u75282\u8fdb\u884c\u7b80\u5316\ncalculate(opt: \"+\"){$0 + $1}\n<\/pre>\n\n\n\n<p>\u2003\u73b0\u5728\u6211\u4eec\u6765\u89e3\u91ca\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u770b\u770b\u5230\u5e95\u4ec0\u4e48\u53eb\u505a\u5c3e\u968f\u95ed\u5305\u3002\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2acalculate()\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570opt\u662fString\u7c7b\u578b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570closureP\u662f\u4e00\u4e2a\u95ed\u5305\u7c7b\u578b\u3002\u7b2c\u4e00\u6b21\u8c03\u7528calculate()\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u662f\u6309\u7167\u6b63\u5e38\u8c03\u7528\u51fd\u6570\u7684\u6b65\u9aa4\uff0c\u5c06opt\u548cclosureP\u8fd9\u4e24\u4e2a\u53c2\u6570\u5206\u522b\u4f20\u5165\u3002\u4f46\u662f\uff0c\u5728\u7b2c\u4e8c\u6b21\u8c03\u7528calculate()\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u53ea\u7ed9\u5b83\u4f20\u9012\u4e86\u7b2c\u4e00\u4e2a\u53c2\u6570opt\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570closureP\u662f\u76f4\u63a5\u632a\u5230\u4e86calculate()\u51fd\u6570\u5c0f\u62ec\u53f7\u7684\u5916\u9762\uff0c\u50cf\u8fd9\u79cd\u5199\u6cd5\u5c31\u662f\u5c3e\u968f\u95ed\u5305\u3002<\/p>\n\n\n\n<p><strong>\u9003\u9038\u95ed\u5305\u548c\u975e\u9003\u9038\u95ed\u5305<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u95ed\u5305\u53ea\u6709\u5728\u51fd\u6570\u4e2d\u505a\u53c2\u6570\u65f6\u624d\u4f1a\u533a\u5206\u9003\u9038\u95ed\u5305\u548c\u975e\u9003\u9038\u95ed\u5305\u3002<br>Swift 3.0\u4e4b\u540e\uff0c\u4f20\u9012\u95ed\u5305\u5230\u51fd\u6570\u4e2d\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u4f1a\u9ed8\u8ba4\u4e3a\u975e\u9003\u9038\u95ed\u5305\u7c7b\u578b\uff08NonescapingClosures)@noescaping\uff0c\u9003\u9038\u95ed\u5305\u5728\u95ed\u5305\u524d\u8981\u6dfb\u52a0@escaping\u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<p>1.<strong>\u4ece\u751f\u547d\u5468\u671f\u770b\u4e24\u8005\u533a\u522b\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u975e\u9003\u9038\u95ed\u5305\u7684\u751f\u547d\u5468\u671f\u4e0e\u51fd\u6570\u76f8\u540c\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1\uff0c\u628a\u95ed\u5305\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u51fd\u6570\uff1b\n2\uff0c\u51fd\u6570\u4e2d\u8c03\u7528\u95ed\u5305\uff1b\n3\uff0c\u9000\u51fa\u51fd\u6570\u3002\u7ed3\u675f<\/pre>\n\n\n\n<ul><li>\u9003\u9038\u95ed\u5305\u7684\u751f\u547d\u5468\u671f<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1\uff0c\u95ed\u5305\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff1b\n2\uff0c\u9000\u51fa\u51fd\u6570\uff1b\n3\uff0c\u95ed\u5305\u88ab\u8c03\u7528\uff0c\u95ed\u5305\u751f\u547d\u5468\u671f\u7ed3\u675f<\/pre>\n\n\n\n<p>\u5373\u9003\u9038\u95ed\u5305\u7684\u751f\u547d\u5468\u671f\u957f\u4e8e\u51fd\u6570\uff0c\u51fd\u6570\u9000\u51fa\u7684\u65f6\u5019\uff0c\u9003\u9038\u95ed\u5305\u7684\u5f15\u7528\u4ecd\u88ab\u5176\u4ed6\u5bf9\u8c61\u6301\u6709\uff0c\u4e0d\u4f1a\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u91ca\u653e<\/p>\n\n\n\n<p><strong>2.\u7ecf\u5e38\u4f7f\u7528\u9003\u9038\u95ed\u5305\u76842\u4e2a\u573a\u666f\uff1a<\/strong><\/p>\n\n\n\n<ul><li><strong>\u5f02\u6b65\u8c03\u7528<\/strong>: \u5982\u679c\u9700\u8981\u8c03\u5ea6\u961f\u5217\u4e2d\u5f02\u6b65\u8c03\u7528\u95ed\u5305\uff0c\u6bd4\u5982\u7f51\u7edc\u8bf7\u6c42\u6210\u529f\u7684\u56de\u8c03\u548c\u5931\u8d25\u7684\u56de\u8c03\uff0c\u8fd9\u4e2a\u961f\u5217\u4f1a\u6301\u6709\u95ed\u5305\u7684\u5f15\u7528\uff0c\u81f3\u4e8e\u4ec0\u4e48\u65f6\u5019\u8c03\u7528\u95ed\u5305\uff0c\u6216\u95ed\u5305\u4ec0\u4e48\u65f6\u5019\u8fd0\u884c\u7ed3\u675f\u90fd\u662f\u4e0d\u786e\u5b9a\uff0c\u4e0a\u8fb9\u7684\u4f8b\u5b50\u3002<\/li><li><strong>\u5b58\u50a8<\/strong>: \u9700\u8981\u5b58\u50a8\u95ed\u5305\u4f5c\u4e3a\u5c5e\u6027\uff0c\u5168\u5c40\u53d8\u91cf\u6216\u5176\u4ed6\u7c7b\u578b\u3002<\/li><\/ul>\n\n\n\n<p><strong>3.\u901a\u8fc7\u4e00\u4e2a\u5b9e\u4f8b\u4ee3\u7801\u6765\u8bb2\u89e3\u9003\u9038\u95ed\u5305\u548c\u975e\u9003\u9038\u95ed\u5305\u7684\u533a\u522b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class HttpTool: NSObject {\n    func loadData(callBack:(String)->())  {   \/\/\/(1)\n        callBack(\"\u975e\u9003\u9038\u95ed\u5305\")              \/\/\/(2)\n    }                                     \/\/\/(3)\n}\n<\/pre>\n\n\n\n<p>\u4ee3\u7801\u6267\u884c\u987a\u5e8f\uff081\uff09\uff0c\uff082\uff09\uff0c\uff083\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class HttpTool: NSObject {\n    func loadData(callBack:@escaping((String)->()))  {   \/\/\/(1)\n        DispatchQueue.global().async {\n            DispatchQueue.main.async {\n                callBack(\"\u975e\u9003\u9038\u95ed\u5305\")              \/\/\/(2)\n            }\n        }\n    }                                             \/\/\/(3)\n}\n<\/pre>\n\n\n\n<p>\u4ee3\u7801\u6267\u884c\u987a\u5e8f:(1)\uff0c(3)\uff0c(2)<\/p>\n\n\n\n<p>\u9003\u9038\u95ed\u5305\u524d\u9762\u6dfb\u52a0@escaping\u5173\u952e\u5b57\uff0c\u8fd9\u91cc\u95ed\u5305\u7684\u751f\u547d\u5468\u671f\u4e0d\u53ef\u9884\u77e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ed\u5305\u7684\u6982\u5ff5 \u4e00\u95e8\u8ba1\u7b97\u673a\u8bed\u8a00\u5982\u679c\u8981\u652f\u6301\u95ed\u5305\uff0c\u5fc5\u987b\u8981\u6709\u4e24\u4e2a\u524d\u63d0\uff1a\u652f\u6301\u51fd\u6570\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ef\u4ee5\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u8fdb\u884c\u4f20\u9012\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7390"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7390"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7390\/revisions"}],"predecessor-version":[{"id":7399,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7390\/revisions\/7399"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}