{"id":136004,"date":"2019-02-18T08:19:31","date_gmt":"2019-02-18T00:19:31","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=136004"},"modified":"2019-02-14T09:19:56","modified_gmt":"2019-02-14T01:19:56","slug":"swift-5-exclusive-forced-memory","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/swift-5-exclusive-forced-memory.html","title":{"rendered":"Swift 5\u5f3a\u5236\u72ec\u5360\u5185\u5b58"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSwift 5 \u5c06\u5e26\u6765\u6539\u8fdb\u7684 Swift \u7a0b\u5e8f\u5185\u5b58\u5b89\u5168\u6027\uff0c\u5728\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u4fee\u6539\u53d8\u91cf\u65f6\uff0c\u4e0d\u5141\u8bb8\u901a\u8fc7\u5176\u4ed6\u53d8\u91cf\u540d\u6765\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002\u8fd9\u4e2a\u53d8\u66f4\u5bf9\u73b0\u6709\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u548c Swift \u7f16\u8bd1\u5668\u672c\u8eab\u90fd\u6709\u91cd\u8981\u5f71\u54cd\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u5728\u591a\u79cd\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u72ec\u5360\u5185\u5b58\u8bbf\u95ee\u95ee\u9898\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u9759\u6001\u5730\u6355\u83b7\u5927\u90e8\u5206\u95ee\u9898\uff0c\u5269\u4e0b\u7684\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u5904\u7406\u3002\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u5904\u7406\u7684\u95ee\u9898\u5305\u62ec\u5177\u6709\u8f6c\u4e49\u95ed\u5305\u3001\u7c7b\u7c7b\u578b\u5c5e\u6027\u3001\u9759\u6001\u5c5e\u6027\u548c\u5168\u5c40\u53d8\u91cf\u7684\u6392\u4ed6\u6027\u8fdd\u89c4\u3002<\/p>\n

\u4e3a\u4e86\u66f4\u597d\u5730\u8bf4\u660e\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4e00\u4e2a\u76f8\u5f53\u666e\u904d\u7684\u60c5\u51b5\uff1a\u4fee\u6539\u4e00\u4e2a\u51fd\u6570\u7684 inout \u53d8\u91cf\uff0c\u8fd9\u4e2a\u51fd\u6570\u6267\u884c\u4e86\u4e00\u4e2a\u95ed\u5305\uff0c\u8fd9\u4e2a\u95ed\u5305\u4f7f\u7528\u540c\u4e00\u4f5c\u7528\u57df\u5185\u7684\u4e24\u4e2a\u4e0d\u540c\u7684\u540d\u79f0\u8bbf\u95ee\u4e0a\u8ff0\u7684\u53d8\u91cf\uff1a<\/p>\n

func modifyTwice(_ value: inout Int, by modifier: (inout Int) -> ()) {\r\n  modifier(&value)\r\n  modifier(&value)\r\n}\r\n\r\nfunc testCount() {\r\n  var count = 1\r\n  modifyTwice(&count) { $0 += count }\r\n  print(count)\r\n}\r\n<\/pre>\n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56e0\u4e3a\u4f7f\u7528 count \u540c\u65f6\u4f5c\u4e3a modifyTwice \u548c modifier \u7684 inout \u53c2\u6570\uff0c\u6240\u4ee5\u51fa\u73b0\u4e86\u95ee\u9898\u3002\u6211\u4eec\u4e0d\u6e05\u695a print \u8bed\u53e5\u5e94\u8be5\u6253\u5370\u51fa\u4ec0\u4e48\u5185\u5bb9\u3002\u7b2c\u4e00\u6b21 count \u53d8\u91cf\u9012\u589e\uff0c\u5b83\u7684\u503c\u9012\u589e\u5230 2\u3002\u4f46\u662f\uff0c\u5f53\u6267\u884c\u7b2c\u4e8c\u6b21\u52a0\u6cd5\u65f6\uff0c\u8981\u6dfb\u52a0\u5230 $0 \u7684 count \u503c\u662f\u591a\u5c11\uff1f\u8fd9\u53ef\u80fd\u53d6\u51b3\u4e8e\u5f88\u591a\u56e0\u7d20\uff0c\u56e0\u4e3a\u5185\u5b58\u64cd\u4f5c\u4e0d\u4e00\u5b9a\u662f\u77ac\u65f6\u7684\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5f15\u5165\u4f18\u5316\uff0c\u8fdb\u4e00\u6b65\u4f7f\u8fd9\u79cd\u60c5\u51b5\u590d\u6742\u5316\u3002<\/p>\n

\u8fd9\u4e2a\u95ee\u9898\u4e0d\u4ec5\u4e0e\u901a\u8fc7\u4e0d\u540c\u53d8\u91cf\u540d\u540c\u65f6\u4fee\u6539\u5185\u5b58\u7684\u4e0d\u53ef\u9884\u6d4b\u6027\u6709\u5173\uff0c\u4e5f\u4e0e\u7f16\u8bd1\u5668\u7684\u590d\u6742\u6027\u6709\u5173\u3002<\/p>\n

\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u548c\u6df7\u4e71\u7684\u7ed3\u679c\u3002\u5b83\u8fd8\u5bfc\u81f4\u7f16\u8bd1\u5668\u548c\u6807\u51c6\u5e93\u7684\u5b9e\u73b0\u5177\u6709\u5f88\u5927\u7684\u4fdd\u5b88\u6027\uff0c\u5b83\u4eec\u901a\u5e38\u5fc5\u987b\u786e\u4fdd\u7a0b\u5e8f\u7684\u57fa\u672c\u53ef\u9760\u6027\uff08\u6ca1\u6709\u5d29\u6e83\u6216\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\uff09\uff0c\u5373\u4f7f\u662f\u5728\u4e0d\u5bfb\u5e38\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n

\u6240\u6709\u8fd9\u4e9b\u610f\u5473\u7740\u5982\u679c\u53d1\u73b0\u72ec\u5360\u8bbf\u95ee\u51b2\u7a81\uff0c\u4f7f\u7528 Swift 5 \u7f16\u8bd1\u5668\u7f16\u8bd1\u7684\u5e94\u7528\u7a0b\u5e8f\u5c06\u5728\u8fd0\u884c\u65f6\u5d29\u6e83\u3002\u8fd9\u4e2a\u884c\u4e3a\u4ee5\u524d\u5728 Swift 4 \u7f16\u8bd1\u5668\u8c03\u8bd5\u6a21\u5f0f\u4e0b\u53ef\u7528\uff0c\u56e0\u6b64\uff0c\u4ec5\u5728\u8fd0\u884c\u65f6\u6a21\u5f0f\u4e0b\u6d4b\u8bd5\u8fc7\u7684\u7a0b\u5e8f\u5728\u4f7f\u7528 Swift 5 \u7f16\u8bd1\u65f6\u6709\u5d29\u6e83\u7684\u98ce\u9669\u3002<\/p>\n

Swift 4 \u7f16\u8bd1\u5668\u53ef\u7528\uff0c\u56e0\u6b64\u4ec5\u5728\u8fd0\u884c\u65f6\u6a21\u5f0f\u4e0b\u6d4b\u8bd5\u7684\u7a0b\u5e8f\u5728\u4f7f\u7528 Swift 5 \u7f16\u8bd1\u65f6\u53ef\u80fd\u4f1a\u5d29\u6e83\u3002<\/p>\n

\u4fee\u590d\u8bbf\u95ee\u72ec\u5360\u8fdd\u89c4\u7684\u4e00\u822c\u65b9\u6cd5\u662f\u590d\u5236\u6570\u636e\u3002\u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u8fd9\u5c06\u5f52\u7ed3\u4e3a\uff1a<\/p>\n

func modifyTwice(_ value: inout Int, by modifier: (inout Int) -> ()) {\r\n  modifier(&value)\r\n  modifier(&value)\r\n}\r\n\r\nfunc testCount() {\r\n  var count = 1\r\n  let increment = count\r\n  modifyTwice(&count) { $0 += increment }\r\n  print(count)\r\n}\r\n<\/pre>\n

\u5b9e\u9645\u4e0a\uff0c\u8bbf\u95ee\u72ec\u5360\u8fdd\u89c4\u68c0\u67e5\u53ef\u80fd\u4f1a\u88ab\u7981\u7528\uff0c\u4f46\u5f3a\u70c8\u5efa\u8bae\u4e0d\u8981\u8fd9\u6837\u505a\uff1a<\/p>\n

\u867d\u7136\u7981\u7528\u8fd0\u884c\u65f6\u68c0\u67e5\u53ef\u80fd\u53ef\u4ee5\u89e3\u51b3\u6027\u80fd\u95ee\u9898\uff0c\u4f46\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u72ec\u5360\u8fdd\u89c4\u662f\u5b89\u5168\u7684\u3002\u5982\u679c\u6ca1\u6709\u542f\u7528\u5f3a\u5236\u6267\u884c\uff0c\u7a0b\u5e8f\u5458\u5fc5\u987b\u9075\u5b88\u72ec\u5360\u89c4\u5219\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u591a\u79cd\u60c5\u51b5\u4e0b\u4f1a\u53d1\u751f\u72ec\u5360\u5185\u5b58\u8bbf\u95ee\u95ee\u9898\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u9759\u6001\u5730\u6355\u83b7\u5927\u90e8\u5206\u95ee\u9898\uff0c\u5269\u4e0b\u7684\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u5904\u7406\u3002\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u5904\u7406\u7684 […]<\/p>\n","protected":false},"author":1329,"featured_media":136007,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-136004","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/136004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=136004"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/136004\/revisions"}],"predecessor-version":[{"id":136008,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/136004\/revisions\/136008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/136007"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=136004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=136004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=136004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}