{"id":123,"date":"2026-05-13T16:58:46","date_gmt":"2026-05-13T08:58:46","guid":{"rendered":"http:\/\/blog.thirdbody.cn\/?p=123"},"modified":"2026-05-13T16:58:46","modified_gmt":"2026-05-13T08:58:46","slug":"swift%e7%b3%bb%e5%88%97%e4%ba%8c%ef%bc%9a%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"http:\/\/blog.thirdbody.cn\/index.php\/2026\/05\/13\/swift%e7%b3%bb%e5%88%97%e4%ba%8c%ef%bc%9a%e9%97%ad%e5%8c%85\/","title":{"rendered":"swift\u7cfb\u5217(\u4e8c)\uff1a\u95ed\u5305"},"content":{"rendered":"<h3>1. \u6982\u8ff0\u548c\u80cc\u666f<\/h3>\n<p><strong>\u95ed\u5305<\/strong> \u662f\u53ef\u4ee5\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u4f20\u9012\u548c\u4f7f\u7528\u7684\u72ec\u7acb\u529f\u80fd\u5757\u3002Swift \u4e2d\u7684\u95ed\u5305\u7c7b\u4f3c\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u533f\u540d\u51fd\u6570\u3001lambda \u8868\u8fbe\u5f0f\u548c\u4ee3\u7801\u5757\u3002<\/p>\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\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u770b\u4f5c\u662f\u5c06\u8fd9\u4e9b\u5e38\u91cf\u548c\u53d8\u91cf <strong>\u5305\u542b<\/strong> \u5728\u95ed\u5305\u7684\u4f5c\u7528\u57df\u5185\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<p>\u5168\u5c40\u51fd\u6570\u548c\u5d4c\u5957\u51fd\u6570\u5b9e\u9645\u4e0a\u4e5f\u662f\u7279\u6b8a\u7684\u95ed\u5305\uff0c\u95ed\u5305\u91c7\u7528\u5982\u4e0b\u4e09\u79cd\u5f62\u5f0f\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li>\u5168\u5c40\u51fd\u6570\u662f\u4e00\u4e2a\u6709\u540d\u5b57\u4f46\u4e0d\u4f1a\u6355\u83b7\u4efb\u4f55\u503c\u7684\u95ed\u5305<\/li>\n<li>\u5d4c\u5957\u51fd\u6570\u662f\u4e00\u4e2a\u6709\u540d\u5b57\u5e76\u53ef\u4ee5\u6355\u83b7\u5176\u5c01\u95ed\u51fd\u6570\u57df\u5185\u503c\u7684\u95ed\u5305<\/li>\n<li>\u95ed\u5305\u8868\u8fbe\u5f0f\u662f\u4f7f\u7528\u8f7b\u91cf\u7ea7\u8bed\u6cd5\u7f16\u5199\u7684\u533f\u540d\u95ed\u5305\uff0c\u5b83\u4eec\u80fd\u591f\u6355\u83b7\u5176\u4e0a\u4e0b\u6587\u4e2d\u7684\u503c\u3002<\/li>\n<\/ul>\n<h3>2. \u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528<\/h3>\n<p>\u672c\u7ae0\u5c06\u4f1a\u57fa\u4e8e<code>sorted(by:)<\/code>\u65b9\u6cd5\u4ecb\u7ecd\u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528<\/p>\n<h4>2.1 \u4e00\u822c\u5f62\u5f0f\u8bed\u6cd5<\/h4>\n<p>\u5bf9\u4e8e<code>sorted(by:)<\/code>\u65b9\u6cd5\u7684<code>by<\/code>\u53c2\u6570\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u53c2\u6570:<\/p>\n<pre><code class=\"language-swift\">var names = [&quot;Chris&quot;, &quot;Alex&quot;, &quot;Ewa&quot;, &quot;Barry&quot;, &quot;Daniella&quot;]\n\nfunc backward(_ str1: String, _ str2: String) -&gt; Bool {\n    return str1 &gt; str2\n}\n\n\/\/ 1. \u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u95ed\u5305\u4f20\u9012\u7ed9sorted\u65b9\u6cd5\nlet sortedNames1 = names.sorted(by: backward)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/202605\/swift-closure-1.png\" alt=\"\" \/><\/p>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u8054\u95ed\u5305\u8868\u8fbe\u5f0f\u4f5c\u4e3a<code>sorted<\/code>\u7684\u95ed\u5305\u53c2\u6570\uff0c\u95ed\u5305\u8868\u8fbe\u5f0f\u7684\u4e00\u822c\u8bed\u6cd5\u5f62\u5f0f\u5982\u4e0b:<\/p>\n<pre><code class=\"language-swift\">{ (&lt;#parameters#&gt;) -&gt; &lt;#return type#&gt; in\n   &lt;#statements#&gt;\n}<\/code><\/pre>\n<p>\u4e0e\u51fd\u6570\u5b9a\u4e49\u7c7b\u4f3c\uff1a<\/p>\n<ul>\n<li>\u6709\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u7c7b\u578b\u548c\u51fd\u6570\u4f53<\/li>\n<\/ul>\n<p>\u4f46\u662f\u4e0e\u51fd\u6570\u5b9a\u4e49\u4e0d\u540c\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\u53c2\u6570\u5217\u8868\u5199\u5728\u82b1\u62ec\u53f7<code>{}<\/code>\u91cc\u9762<\/li>\n<li>\u7528\u5173\u952e\u5b57<code>in<\/code>\u5c06\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u7c7b\u578b\u4e0e\u51fd\u6570\u4f53\u9694\u5f00<\/li>\n<\/ul>\n<p>\u5373\u4e0a\u8ff0<code>sorted<\/code>\u65b9\u6cd5\u7684\u8c03\u7528\u53ef\u4ee5\u5199\u6210\u4e00\u4e0b\u5f62\u5f0f:<\/p>\n<pre><code class=\"language-swift\">var names = [&quot;Chris&quot;, &quot;Alex&quot;, &quot;Ewa&quot;, &quot;Barry&quot;, &quot;Daniella&quot;]\n\nfunc backward(_ str1: String, _ str2: String) -&gt; Bool {\n    return str1 &gt; str2\n}\n\n\/\/ 2. \u4f7f\u7528\u5b8c\u6574\u7684\u95ed\u5305\u8868\u8fbe\u5f0f\u8bed\u6cd5\u4f20\u9012\u95ed\u5305\u7ed9sorted\u51fd\u6570\nlet sortedNames2 = names.sorted(by: {(str1: String, str2: String) -&gt; Bool in\nreturn str1 &gt; str2})<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/202605\/swift-closure-2.png\" alt=\"\" \/><\/p>\n<h4>2.2 \u7c7b\u578b\u63a8\u65ad<\/h4>\n<p>\u5f53\u95ed\u5305\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u65b9\u6cd5\u7684\uff0c Swift \u53ef\u4ee5\u6839\u636e\u8c03\u7528\u4e0a\u4e0b\u6587\u63a8\u65ad\u5176\u53c2\u6570\u7684\u7c7b\u578b\u548c\u8fd4\u56de\u7684\u503c\u7684\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-swift\">var names = [&quot;Chris&quot;, &quot;Alex&quot;, &quot;Ewa&quot;, &quot;Barry&quot;, &quot;Daniella&quot;]\n\nfunc backward(_ str1: String, _ str2: String) -&gt; Bool {\n    return str1 &gt; str2\n}\n\n\/\/ 3. \u7531\u4e8e\u6392\u5e8f\u95ed\u5305\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9sorted\u7684\uff0c\u6240\u4ee5\u5b83\u80fd\u591f\u6839\u636e\u4e0a\u4e0b\u6587\u63a8\u65ad\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\nlet sortedNames3 = names.sorted(by: {str1, str2 in return str1 &lt; str2})<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/202605\/swift-closure-3.png\" alt=\"\" \/><\/p>\n<h4>2.3 \u9690\u5f0f\u8fd4\u56de<\/h4>\n<p>\u5f53\u95ed\u5305\u4ec5\u5305\u542b\u4e00\u6761\u8fd4\u56de\u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u7701\u7565<code>return<\/code>\u5173\u952e\u5b57:<\/p>\n<pre><code class=\"language-swift\">var names = [&quot;Chris&quot;, &quot;Alex&quot;, &quot;Ewa&quot;, &quot;Barry&quot;, &quot;Daniella&quot;]\n\nfunc backward(_ str1: String, _ str2: String) -&gt; Bool {\n    return str1 &gt; str2\n}\n\n\/\/ 4. \u5355\u8868\u8fbe\u5f0f\u95ed\u5305\u53ef\u4ee5\u901a\u8fc7\u4ece\u5176\u58f0\u660e\u4e2d\u7701\u7565 return \u5173\u952e\u5b57\u6765\u9690\u5f0f\u8fd4\u56de\u5176\u5355\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\nlet sortedNames4 = names.sorted(by: {str1, str2 in str1 &gt; str2})<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/202605\/swift-closure-4.png\" alt=\"\" \/><\/p>\n<h4>2.4 \u7b80\u5199\u53c2\u6570\u540d\u79f0<\/h4>\n<p><strong>Swift<\/strong>\u81ea\u52a8\u4e3a\u5185\u8054\u95ed\u5305\u63d0\u4f9b\u4e86\u7b80\u5199\u53c2\u6570\u540d\u79f0\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 $0\u3001$1\u3001$2 \u7b49\u6765\u5f15\u7528\u95ed\u5305\u53c2\u6570\u7684\u503c\uff0c\u4ee5\u6b64\u7c7b\u63a8:<\/p>\n<pre><code class=\"language-swift\">var names = [&quot;Chris&quot;, &quot;Alex&quot;, &quot;Ewa&quot;, &quot;Barry&quot;, &quot;Daniella&quot;]\n\nfunc backward(_ str1: String, _ str2: String) -&gt; Bool {\n    return str1 &gt; str2\n}\n\n\/\/ 5. Swift \u81ea\u52a8\u4e3a\u5185\u8054\u95ed\u5305\u63d0\u4f9b\u4e86\u7b80\u5199\u53c2\u6570\u540d\u79f0\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 $0\u3001$1\u3001$2 \u7b49\u6765\u5f15\u7528\u95ed\u5305\u53c2\u6570\u7684\u503c\uff0c\u4ee5\u6b64\u7c7b\u63a8\nlet sortedNames5 = names.sorted(by: {$0 &lt; $1})<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/202605\/swift-closure-5.png\" alt=\"\" \/><\/p>\n<h4>2.5 \u5c3e\u968f\u95ed\u5305<\/h4>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5c06\u95ed\u5305\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5e76\u4e14\u95ed\u5305\u8868\u8fbe\u5f0f\u5f88\u957f\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u7f16\u5199\u4e3a \u5c3e\u968f\u95ed\u5305\uff1a\u5728\u51fd\u6570\u8c03\u7528\u7684\u62ec\u53f7\u540e\u7f16\u5199\u4e00\u4e2a\u5c3e\u968f\u95ed\u5305\uff0c\u8be5\u5c3e\u968f\u95ed\u5305\u4ecd\u7136\u4f1a\u4f5c\u4e3a\u8be5\u51fd\u6570\u7684\u4e00\u4e2a\u53c2\u6570<\/p>\n<p>\u5f53\u4e00\u4e2a\u95ed\u5305\u8868\u8fbe\u5f0f\u662f\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u552f\u4e00\u53c2\u6570\uff0c\u5e76\u4e14\u628a\u8be5\u8868\u8fbe\u5f0f\u5199\u4f5c\u5c3e\u968f\u95ed\u5305\uff0c\u5219\u5728\u8c03\u7528\u51fd\u6570\u6216\u65b9\u6cd5\u65f6\uff0c\u65e0\u9700\u5728\u51fd\u6570\u540d\u6216\u65b9\u6cd5\u540d\u540e\u7f16\u5199\u4e00\u5bf9\u62ec\u53f7 ()<\/p>\n<pre><code class=\"language-swift\">var names = [&quot;Chris&quot;, &quot;Alex&quot;, &quot;Ewa&quot;, &quot;Barry&quot;, &quot;Daniella&quot;]\n\nfunc backward(_ str1: String, _ str2: String) -&gt; Bool {\n    return str1 &gt; str2\n}\n\n\/\/ 6. \u5982\u679c\u4f60\u9700\u8981\u5c06\u95ed\u5305\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5e76\u4e14\u95ed\u5305\u8868\u8fbe\u5f0f\u5f88\u957f\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u7f16\u5199\u4e3a \u5c3e\u968f\u95ed\u5305\uff1a\u5728\u51fd\u6570\u8c03\u7528\u7684\u62ec\u53f7\u540e\u7f16\u5199\u4e00\u4e2a\u5c3e\u968f\u95ed\u5305\uff0c\u8be5\u5c3e\u968f\u95ed\u5305\u4ecd\u7136\u4f1a\u4f5c\u4e3a\u8be5\u51fd\u6570\u7684\u4e00\u4e2a\u53c2\u6570\n\/\/ \u5f53\u4e00\u4e2a\u95ed\u5305\u8868\u8fbe\u5f0f\u662f\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u552f\u4e00\u53c2\u6570\uff0c\u5e76\u4e14\u628a\u8be5\u8868\u8fbe\u5f0f\u5199\u4f5c\u5c3e\u968f\u95ed\u5305\uff0c\u5219\u5728\u8c03\u7528\u51fd\u6570\u6216\u65b9\u6cd5\u65f6\uff0c\u65e0\u9700\u5728\u51fd\u6570\u540d\u6216\u65b9\u6cd5\u540d\u540e\u7f16\u5199\u4e00\u5bf9\u62ec\u53f7 ()\nlet sortedNames6 = names.sorted {$0 &gt; $1}<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/202605\/swift-closure-6.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6982\u8ff0\u548c\u80cc\u666f \u95ed\u5305 \u662f\u53ef\u4ee5\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u4f20\u9012\u548c\u4f7f\u7528\u7684\u72ec\u7acb\u529f\u80fd\u5757\u3002Swift \u4e2d\u7684\u95ed\u5305\u7c7b\u4f3c\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u533f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[20,67,66],"class_list":["post-123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-swift","tag-67","tag-66"],"_links":{"self":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":1,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":125,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions\/125"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media\/124"}],"wp:attachment":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}