{"id":4076,"date":"2026-01-20T16:45:06","date_gmt":"2026-01-20T07:45:06","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=4076"},"modified":"2026-01-20T19:04:31","modified_gmt":"2026-01-20T10:04:31","slug":"swiftclouser","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/01\/20\/swiftclouser\/","title":{"rendered":"[Swift]Closure\/\ud074\ub85c\uc800"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \ud074\ub85c\uc800\uc758 \uc758\ubbf8 \/ Meaning of closure<\/p>\n\n\n\n<p>\u2714\ufe0f \ucf54\ub4dc \ube14\ub85d \uadf8 \uc790\uccb4\ub97c \ubcc0\uc218\ucc98\ub7fc \uc804\ub2ec\ud558\uac70\ub098 \uc0ac\uc6a9\ud560 \uc218 \uc788\ub294 &#8220;\uc774\ub984 \uc5c6\ub294 \ud568\uc218&#8221;\ub77c\uace0 \uc774\ud574\ud558\uba74\ub429\ub2c8\ub2e4.<br>You can think of it as an &#8220;unnamed function&#8221; where the code block itself can be passed or used like a variable.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ud074\ub85c\uc800\uc758 \uae30\ubcf8\ud615\uc2dd \/ Basic format of closure<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{ (\ub9e4\uac1c\ubcc0\uc218\/Parameters) -> \ubc18\ud658\ud0c0\uc785\/conversion types in\n    \uc2e4\ud589 \ucf54\ub4dc\/execution code\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798 \ucf54\ub4dc\uc5d0\uc11c \ud074\ub85c\uc800\uc758 \uc0ac\uc6a9 \/ Use of closure in the code below<\/p>\n\n\n\n<p>\u2714\ufe0f \ubc84\ud2bc\ud074\ub9ad\uc2dc \uba54\uc138\uc9c0\ubcc0\uacbd(\uc77c\ubc18\uc801\uc778 \ud615\ud0dc)<br>Change message when button is clicked (general form)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            Button(action: {\n                self.message = \"\ud074\ub85c\uc800 \uc2e4\ud589 \uc644\ub8cc\/Closure execution complete!\"\n            }) {\n                Text(\"\uba54\uc2dc\uc9c0 \ubcc0\uacbd\/Change message\")\n                    .padding()\n                    .background(Color.blue)\n                    .foregroundStyle(.white)\n                    .cornerRadius(10)\n            }<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ud6c4\ud589 \ud074\ub85c\uc800 \/ trailing closure<\/p>\n\n\n\n<p>&#8212; Swift\uc5d0\uc11c \ud568\uc218\uc758 \ub9c8\uc9c0\ub9c9 \uc778\uc790\uac00 \ud074\ub85c\uc800(\ud568\uc218 \ube14\ub85d)\uc77c \ub54c, \uadf8 \ud074\ub85c\uc800\ub97c \uc18c\uad04\ud638\u00a0<code>()<\/code>\u00a0\ubc14\uae65\uc73c\ub85c \ube7c\ub0b4\uc5b4 \uc911\uad04\ud638\u00a0<code>{}<\/code>\ub85c \uc791\uc131\ud560 \uc218 \uc788\uac8c \ud574\uc8fc\ub294 \ubb38\ubc95\uc785\ub2c8\ub2e4.<br>In Swift, when the last argument of a function is a closure (function block), this is a syntax that allows you to take the closure out of the parentheses () and write it in curly brackets {}.<\/p>\n\n\n\n<p>&#8212; \uc77c\ubc18\uc801\uc778\ud615\ud0dc \/ general form<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Button(title: \"\ucd08\uae30\ud654\/reset\", action: {\n    resetAll()\n})<\/code><\/pre>\n\n\n\n<p>&#8212;  \ud6c4\ud589 \ud074\ub85c\uc800 \uc801\uc6a9 \/ Apply trailing closure<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Button(\"\ucd08\uae30\ud654\/reset\") {\n    resetAll()\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ud074\ub85c\uc800 \ucea1\ucdb0 \/ Closure Capture<\/p>\n\n\n\n<p>&#8212;  \ud074\ub85c\uc800\ub97c  \ubcc0\uc218\uc5d0 \uc800\uc7a5\ud574\uc11c \ud638\ucd9c\ud558\uba74  \ud074\ub85c\uc800 \ub0b4\ubd80\uc758 \ubcc0\uc218\ub294 \uacc4\uc18d \uae30\uc5b5\ub429\ub2c8\ub2e4.(\ud074\ub85c\uc800 \ucea1\ucdb0)<br>When you call a closure by saving it in a variable, the variables inside the closure are remembered. (Closure capture)<\/p>\n\n\n\n<p>&#8212; init() \ud568\uc218 \/ init() function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    init() {\n        \/\/ \ucd08\uae30\ud654 \uc2dc\uc810\uc5d0 2\uc529 \uc99d\uac00\ud558\ub294 \ud074\ub85c\uc800\ub97c \uc0dd\uc131\ud558\uc5ec \ud560\ub2f9\n        \/\/ \ucd08\uae30\ud654 \ubc84\ud2bc \ud0ed\ud558\uae30 \uc804\uae4c\uc9c0 \uc791\ub3d9\n        \n        \/\/ Create and assign a closure that increments by 2 at initialization time. \/\/ Works until the initialization button is pressed.\n        _incrementByTwo = State(initialValue: {\n            var total = 0\n            return {\n                total += 2\n                return total\n            }\n        }())\n    }<\/code><\/pre>\n\n\n\n<p>&#8212; \ud074\ub85c\uc800 \ud568\uc218 \ud638\ucd9c :  incrementByTwo()\ub97c \uc2e4\ud589\ud558\uba74 total\ud568\uc218\uc758 \uac12\uc774 \uacc4\uc18d \ub204\uc801\ub429\ub2c8\ub2e4.<br>Calling the closure function: When incrementByTwo() is executed, the value of the total function continues to accumulate.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            Button(action: {\n                \/\/ \ud604\uc7ac \ud560\ub2f9\ub41c \ud074\ub85c\uc800\ub97c \uc2e4\ud589\ud558\uc5ec UI \uc5c5\ub370\uc774\ud2b8\n                \/\/ Update the UI by executing the currently assigned closure\n                message2 = String(incrementByTwo())\n            }) {\n                Text(\"\uac12 \ubcc0\uacbd \/ change value (+2)\")\n                    .padding()\n                    .background(Color.green)\n                    .foregroundStyle(.white)\n                    .cornerRadius(10)\n            }<\/code><\/pre>\n\n\n\n<p>\u2b50\ufe0f\uc774 \ubd80\ubd84\uc5d0\uc11c @State\ub85c \uc120\uc5b8\ub41c incrementByTwo\ub97c \ucd08\uae30\ud654 \ud558\uba70 \uc544\ub798\ub294 \uc6b0\ub9ac\uac00 \uc0ac\uc6a9\ud558\ub294 \ucf54\ub4dc\uc640 \ucef4\ud30c\uc77c\ub7ec\uac00 \uc5b4\ub5bb\uac8c \ud574\uc11d\ud558\ub294\uc9c0\ub97c \ubcf4\uc5ec\uc8fc\ub294 \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>In this part, we initialize incrementByTwo declared with @State, and below is the code that we use and how the compiler interprets it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \uc6b0\ub9ac\uac00 \uc4f0\ub294 \ucf54\ub4dc\n@State var message: String = \"Hello\"\n\n\/\/ \uc2e4\uc81c\ub85c \ucef4\ud30c\uc77c\ub7ec\uac00 \ud574\uc11d\ud558\ub294 \ubaa8\uc2b5 (\uac1c\ub150\uc801)\nvar _message: State&lt;String&gt; = State(initialValue: \"Hello\")<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc804\uccb4 \ucf54\ub4dc \/ Full Code<\/p>\n\n\n\n<p>\u2714\ufe0fContentview.swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\nstruct ContentView: View {\n    @State private var message: String = \"Hello, world!\"\n    @State private var message2: String = \"0\"\n    \n    \/\/ \uc774 \ubcc0\uc218\ub294 \"\uc22b\uc790\ub97c \uc99d\uac00\uc2dc\ud0a4\ub294 \ud568\uc218\" \uc790\uccb4\ub97c \ub2f4\uace0 \uc788\uc2b5\ub2c8\ub2e4.\n    \/\/ This variable contains the \"function that increments the number\" itself.\n    @State private var incrementByTwo: () -> Int\n    \n    let apptitle = \"Closure Example\/\ud074\ub85c\uc800 \uc608\uc81c\"\n    \n    init() {\n        \/\/ \ucd08\uae30\ud654 \uc2dc\uc810\uc5d0 2\uc529 \uc99d\uac00\ud558\ub294 \ud074\ub85c\uc800\ub97c \uc0dd\uc131\ud558\uc5ec \ud560\ub2f9\n        \/\/ \ucd08\uae30\ud558 \ubc84\ud2bc \ub0c5\ud558\uae30 \uc804\uae4c\uc9c0 \uc791\ub3d9\n        \n        \/\/ Create and assign a closure that increments by 2 at initialization time. \/\/ Works until the initialization button is pressed.\n        _incrementByTwo = State(initialValue: {\n            var total = 0\n            return {\n                total += 2\n                return total\n            }\n        }())\n    }\n    \n    var body: some View {\n        VStack(spacing: 20) {\n            Image(systemName: \"globe\")\n                .imageScale(.large)\n                .foregroundStyle(.tint)\n            Text(apptitle)\n                .font(.title)\n            \n            Text(message)\n                .font(.title)\n            Text(message2)\n                .font(.title2)\n\n            Button(action: {\n                self.message = \"\ud074\ub85c\uc800 \uc2e4\ud589 \uc644\ub8cc\/Closure execution complete!\"\n            }) {\n                Text(\"\uba54\uc2dc\uc9c0 \ubcc0\uacbd\/Change message\")\n                    .padding()\n                    .background(Color.blue)\n                    .foregroundStyle(.white)\n                    .cornerRadius(10)\n            }\n            \n            \/\/ \ucd08\uae30\ud654 \ubc84\ud2bc \uc218\uc815\n            \/\/ Modify reset button\n            Button(\"\ucd08\uae30\ud654\/reset\") {\n                resetAll()\n            }\n            .padding()\n            .background(Color.red.opacity(0.8))\n            .foregroundStyle(.white)\n            .cornerRadius(10)\n            \n            Button(action: {\n                \/\/ \ud604\uc7ac \ud560\ub2f9\ub41c \ud074\ub85c\uc800\ub97c \uc2e4\ud589\ud558\uc5ec UI \uc5c5\ub370\uc774\ud2b8\n                \/\/ Update the UI by executing the currently assigned closure\n                message2 = String(incrementByTwo())\n            }) {\n                Text(\"\uac12 \ubcc0\uacbd (+2)\")\n                    .padding()\n                    .background(Color.green)\n                    .foregroundStyle(.white)\n                    .cornerRadius(10)\n            }\n        }\n        .padding()\n    }\n    \n    \/\/ \ubaa8\ub4e0 \uc0c1\ud0dc\ub97c \ucc98\uc74c\uc73c\ub85c \ub418\ub3cc\ub9ac\ub294 \ud568\uc218\n    \/\/ Function that returns all states to the beginning\n    func resetAll() {\n        self.message = \"Hello, world!\"\n        self.message2 = \"0\"\n        \n        \/\/ incrementByTwo\uc5d0 \"\uc0c8\ub85c\uc6b4\" \ud074\ub85c\uc800\ub97c \ud560\ub2f9\ud569\ub2c8\ub2e4.\n        \/\/ \uae30\uc874 \ud074\ub85c\uc800 \uc548\uc5d0 \uc788\ub358 total \ubcc0\uc218(\uba54\ubaa8\ub9ac)\ub97c \ubc84\ub9ac\uace0, 0\ubd80\ud130 \uc2dc\uc791\ud558\ub294 \uc0c8 total\uc744 \ub9cc\ub4ed\ub2c8\ub2e4.\n        \/\/ Assign a \"new\" closure to incrementByTwo.\n        \/\/ Discard the total variable (memory) inside the old closure, and create a new total that starts from 0.\n        self.incrementByTwo = makeIncrementer(for: 2)\n    }\n    \n    \/\/ \ud074\ub85c\uc800\ub97c \uc0dd\uc131\ud574\uc11c \ubc18\ud658\ud558\ub294 \ud329\ud1a0\ub9ac \ud568\uc218(\ucd08\uae30\ud654\ubc84\ud2bc \ud0ed\ud55c \ud6c4 \uc2e4\ud589)\n    \/\/ Factory function that creates and returns a closure (runs after tapping the initialize button)\n    func makeIncrementer(for amount: Int) -> () -> Int {\n        var total = 0\n        return {\n            total += amount\n            return total\n        }\n    }\n}\n#Preview {\n    ContentView()\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\ud06c\ub9b0 \uc0f7 \/ ScreenShot<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"1024\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/closure-481x1024.png\" alt=\"\" class=\"wp-image-4105\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/closure-481x1024.png 481w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/closure-141x300.png 141w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/closure-400x851.png 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/closure.png 482w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \ud074\ub85c\uc800\uc758 \uc758\ubbf8 \/ Meaning of closure \u2714\ufe0f \ucf54\ub4dc \ube14\ub85d \uadf8 \uc790\uccb4\ub97c \ubcc0\uc218\ucc98\ub7fc \uc804\ub2ec\ud558\uac70\ub098 \uc0ac\uc6a9\ud560 \uc218 \uc788\ub294 &#8220;\uc774\ub984 \uc5c6\ub294 \ud568\uc218&#8221;\ub77c\uace0 \uc774\ud574\ud558\uba74\ub429\ub2c8\ub2e4.You can think of it as an &#8220;unnamed function&#8221; where the code block itself can be passed or used like a variable. \ud83d\udc49\ud83c\udffb \ud074\ub85c\uc800\uc758 \uae30\ubcf8\ud615\uc2dd \/ Basic format of closure \ud83d\udc49\ud83c\udffb \uc544\ub798 \ucf54\ub4dc\uc5d0\uc11c \ud074\ub85c\uc800\uc758 \uc0ac\uc6a9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,1],"tags":[],"class_list":["post-4076","post","type-post","status-publish","format-standard","hentry","category-swift","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/4076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=4076"}],"version-history":[{"count":29,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/4076\/revisions"}],"predecessor-version":[{"id":4106,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/4076\/revisions\/4106"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=4076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=4076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=4076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}