{"id":396,"date":"2021-09-17T15:22:52","date_gmt":"1970-01-01T00:00:00","guid":{"rendered":"http:\/\/swift.tecc0.com\/?p=396"},"modified":"2021-09-17T15:22:52","modified_gmt":"1970-01-01T00:00:00","slug":"post-396","status":"publish","type":"post","link":"https:\/\/swift.tecc0.com\/?p=396","title":{"rendered":"Swift\u3067defer\u3092\u4f7f\u3046\u65b9\u6cd5"},"content":{"rendered":"<p>\u3053\u3061\u3089\u3082guard\u3068\u4e00\u7dd2\u3067Swift2.0\u304b\u3089\u4f7f\u3048\u308b\u65b0\u3057\u3081\u306e\u3082\u306e\u3002<\/p>\n<p><strong>defer<\/strong>\u306f\u30b9\u30b3\u30fc\u30d7\u3092\u629c\u3051\u51fa\u3059\u969b\u306b\u3001\u5b9f\u884c\u3055\u308c\u308b\u51e6\u7406\u3092\u5b9a\u7fa9\u3067\u304d\u308b\u3088\u3002<\/p>\n<h3>Swift\u3067defer\u3092\u4f7f\u3046<\/h3>\n<pre class=\"lang:swift decode:true \">var tmp = \"\"\nfor i in 0...2 {\n    defer {\n        print(\"\\(tmp)\u3067\u3059\")\n    }\n    \n    guard i &lt;= 1 else {\n        tmp = String(i)\n        continue\n    }\n    \n    tmp = String(i)\n}\n\/\/ 0\u3067\u3059\n\/\/ 1\u3067\u3059\n\/\/ 2\u3067\u3059<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067tmp\u306bnum\u306e\u6587\u5b57\u5217\u304c\u683c\u7d0d\u3055\u308c\u305f\u3042\u3068\u306b\u3001\u305d\u308c\u305e\u308c<strong>defer<\/strong>\u304c\u52d5\u3044\u3066print\u304c\u52d5\u4f5c\u3057\u3066\u308b\u3088\u3002<\/p>\n<p>\u6ce8\u610f\u70b9\u304c\u3042\u3063\u3066\u3001<strong>defer<\/strong>\u306e\u5ba3\u8a00\u3059\u308b\u4f4d\u7f6e\u3067\u51e6\u7406\u304c\u7570\u306a\u308b\u306e\u3067\u88dc\u8db3\u3059\u308b\u3088\u3002<\/p>\n<h3>Swift\u3067defer\u306e\u51e6\u7406\u306e\u30bf\u30a4\u30df\u30f3\u30b0<\/h3>\n<pre class=\"lang:swift decode:true\">var tmp: String\nfor i in 0...2 {\n    guard i &lt;= 1 else {\n        tmp = String(i)\n        continue\n    }\n    \n    tmp = String(i)\n    \n    defer {\n        print(\"\\(tmp)\u3067\u3059\")\n    }\n}\n\/\/ 0\u3067\u3059\n\/\/ 1\u3067\u3059<\/pre>\n<p>\u4e0a\u3068\u898b\u6bd4\u3079\u305f\u3089\u308f\u304b\u308b\u3088\u3046\u306b\u3001<strong>defer<\/strong>\u3092\u4e0b\u306b\u79fb\u52d5\u3057\u305f\u3060\u3051\u3060\u3088\u3002<\/p>\n<p>\u3067\u3082\u4e0a\u3068\u51fa\u529b\u7d50\u679c\u304c\u7570\u306a\u3063\u3066\u308b\u306e\u304c\u308f\u304b\u308b\u3068\u601d\u3046\u3002<\/p>\n<p><strong>defer<\/strong>\u3068\u3044\u3046\u306e\u306f\u3042\u304f\u307e\u3067\u3001<strong>\u51e6\u7406\u3092\u30b9\u30b3\u30fc\u30d7\u304c\u7d42\u308f\u308b\u307e\u3067\u5f85\u3064<\/strong>\u3068\u3044\u3046\u51e6\u7406\u306a\u306e\u3067\u3001defer\u304c\u5f85\u3061\u59cb\u3081\u308b\u524d\u306bbreak\u3084return\u306a\u3069\u3067\u30b9\u30b3\u30fc\u30d7\u3092\u629c\u3051\u3066\u3057\u307e\u3046\u3068\u5b9f\u884c\u3055\u308c\u306a\u3044\u3088\u3002<\/p>\n<p>(\u4eca\u56de\u306fi\u304c2\u306e\u3068\u304d\u306b\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u3066\u3001\u30b9\u30b3\u30fc\u30d7\u304b\u3089\u629c\u3051\u3066\u308b\u3088)<\/p>\n<h4>\u304a\u307e\u3051: defer\u3092\u8907\u6570\u5b9a\u7fa9\u3059\u308b<\/h4>\n<pre class=\"lang:swift decode:true  \">var tmp = \"\"\nfor i in 0...2 {\n    defer {\n        print(\"\\(tmp)\u304a\u3059\u3057\")\n    }\n    \n    defer {\n        tmp = \"\\(tmp)\u3067\u3059\u3057\"\n    }\n    \n    guard i &lt;= 1 else {\n        tmp = String(i)\n        continue\n    }\n    \n    tmp = String(i)\n}\n\/\/ 0\u3067\u3059\u3057\u304a\u3059\u3057\n\/\/ 1\u3067\u3059\u3057\u304a\u3059\u3057\n\/\/ 2\u3067\u3059\u3057\u304a\u3059\u3057<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u8907\u6570defer\u3092\u5b9a\u7fa9\u3057\u3066\u3082\u554f\u984c\u306a\u304f\u52d5\u304f\u3088\u3002<\/p>\n<p>\u51fa\u529b\u7d50\u679c\u3092\u898b\u305f\u3089\u308f\u304b\u308b\u3088\u3046\u306b\u3001\u4e0b\u306b\u5ba3\u8a00\u3057\u305f\u307b\u3046\u304c\u5148\u306b\u52d5\u304f\u3088\u3002<\/p>\n<p><strong>defer<\/strong>\u306f<strong>guard<\/strong>\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u3001<span style=\"color: #ff0000;\">\u5fc5\u9808\u6761\u4ef6\u3092\u6e80\u305f\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u306e\u51e6\u7406\u3092defer\u306b\u8a18\u8ff0\u3059\u308b<\/span>\u3068\u3044\u3063\u305f\u30ca\u30a6\u3044\u611f\u3058\u3067\u4f7f\u3048\u308b\u3088\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3061\u3089\u3082guard\u3068\u4e00\u7dd2\u3067Swift2.0\u304b\u3089\u4f7f\u3048\u308b\u65b0\u3057\u3081\u306e\u3082\u306e\u3002 defer\u306f\u30b9\u30b3\u30fc\u30d7\u3092\u629c\u3051\u51fa\u3059\u969b\u306b\u3001\u5b9f\u884c\u3055\u308c\u308b\u51e6\u7406\u3092\u5b9a\u7fa9\u3067\u304d\u308b\u3088\u3002 Swift\u3067defer\u3092\u4f7f\u3046 var tmp = &#8220;&#8221; for i in 0&#8230;2&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/396"}],"collection":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396"}],"version-history":[{"count":6,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":833,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions\/833"}],"wp:attachment":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}