{"id":2854,"date":"2025-11-25T14:20:44","date_gmt":"2025-11-25T05:20:44","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2854"},"modified":"2025-12-09T11:46:20","modified_gmt":"2025-12-09T02:46:20","slug":"swift-variables-and-constants","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/11\/25\/swift-variables-and-constants\/","title":{"rendered":"[Swift]\ubcc0\uc218\uc640 \uc0c1\uc218 \/ Variables and Constants"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb  \uc544\ub798\ub294 \ubcc0\uc218\uc640 \uc0c1\uc218\ub97c \uc0ac\uc6a9\ud574\uc11c \ud654\uba74\uacfc \ud130\ubbf8\ub110\uc5d0 \uac12\uc744 \ucd9c\ub825\ud558\ub294 \uc571\uc785\ub2c8\ub2e4.<br>Below is an app that uses variables and constants to output values \u200b\u200bto the screen and terminal.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\ub780 \uc5b4\ub5a4 \uac12\uc744 \ub2f4\ub294 \uadf8\ub987\uacfc \uac19\uc73c\uba70 \ucd5c\ucd08\uc5d0 \uac12\uc774 \ud560\ub2f9\ub41c \ud6c4(\ucd08\uae30\ud654\ub77c\uace0 \ud568) \uac12\uc758 \ubcc0\uacbd\uc774 \uac00\ub2a5\uc740 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.<br>A variable is like a container that holds a value, and it means that the value can be changed after it is initially assigned a value (called initialization).<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\uc0c1\uc218\ub780 \uc5b4\ub5a4 \uac12\uc744 \ub2f4\ub294 \uadf8\ub987\uacfc \uac19\uc73c\uba70 \ucd5c\ucd08\uc5d0 \uac12\uc774 \ud560\ub2f9\ub41c \ud6c4(\ucd08\uae30\ud654\ub77c\uace0 \ud568) \uac12\uc744 \ubcc0\uacbd \ud560 \uc218 \uc5c6\ub294 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.<br>A constant is like a container that holds a value, and after a value is initially assigned to it (called initialization), its value cannot be changed.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\uc640 \uc0c1\uc218 \uc120\uc5b8<br>Declare variables and constants<\/p>\n\n\n\n<p>\u2714\ufe0f Swift\uc5d0\uc11c \uc0c1\uc218\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \ud615\uc2dd\uc73c\ub85c \uc120\uc5b8\ud569\ub2c8\ub2e4.<br>In Swift, constants are declared in the following format:<\/p>\n\n\n\n<p>&#8212; \ud0c0\uc785\ucd94\ub860\uc774 \uac00\ub2a5\ud574\uc11c \ud0c0\uc785\uc744 \uc0dd\ub7b5\ud574\uc11c \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Because type inference is possible, you can omit the type and use it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let \uc0c1\uc218\uba85&#91;constant name] : \ud0c0\uc785&#91;Type] = \uac12&#91;Value]\n\nlet \uc0c1\uc218\uba85&#91;constant name] = \uac12&#91;Value]<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Swift\uc5d0\uc11c \ubcc0\uc218 \uc120\uc5b8\uc740 \ub2e4\uc74c\uacfc \uac19\uc740 \ud615\uc2dd\uc73c\ub85c \uc120\uc5b8\ud569\ub2c8\ub2e4.<br>In Swift, variable declarations are declared in the following format:<\/p>\n\n\n\n<p>&#8212; \ud0c0\uc785\ucd94\ub860\uc774 \uac00\ub2a5\ud574\uc11c \ud0c0\uc785\uc744 \uc0dd\ub7b5\ud574\uc11c \uc0ac\uc6a9 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Because type inference is possible, you can use it by omitting the type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var \ubcc0\uc218\uba85&#91;variable name] : \ud0c0\uc785&#91;Type] = \uac12&#91;Value]\n\nvar \ubcc0\uc218\uba85&#91;variable name] = \uac12&#91;Value]<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\uc640 \uc0c1\uc218\uc758 \uc0ac\uc6a9\ubc29\ubc95 \uc608<br>Examples of how to use variables and constants<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \ubcc0\uc218 \/ Variable\nvar countVar2 = 10       \/\/ \ucd08\uae30\ud654 \/ Initialization\n    countVar2 = 10 + 1   \/\/ \uac12\uc758 \ubcc0\uacbd \uac00\ub2a5 \/ Values \u200b\u200bcan be changed\n\n\/\/ \uc0c1\uc218 \/ Constant\nlet countLet2:Int\n    countLet2 = 20       \/\/ \ucd08\uae30\ud654 \/ Initialization\n    countLet2 = 20 + 1   \/\/ \uc5d0\ub7ec\ubc1c\uc0dd \/ Error occurred\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Swift\uc5d0\uc11c \ubcc0\uc218\ub098 \uc0c1\uc218 \uc0ac\uc6a9\uc2dc \uc8fc\uc758 \uc0ac\ud56d<br>Things to keep in mind when using variables and constants in Swift<\/p>\n\n\n\n<p>\u2714\ufe0f Swift\uc5d0\uc11c\ub294 body\uc548\uc5d0 \ubcc0\uc218 \uc120\uc5b8 \ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.<br>In Swift, you cannot declare variables inside the body.<\/p>\n\n\n\n<p>\u2714\ufe0f \ubcc0\uc218\ub098 \uc0c1\uc218\ub97c \uc0ac\uc6a9\uc2dc \uc544\ub798\uc640 \uac19\uc740 \ucf54\ub4dc \ud615\uc2dd\uc740 \uc624\ub958\ub97c \ubc1c\uc0dd \uc2dc\ud0b5\ub2c8\ub2e4.<br>When using variables or constants, the following code format will cause an error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var countVar = 10\nvar changeVarCount: Int = countVar + 1 --&gt; error\n\nlet countLet = 10\nlet countLetCount: Int = countLet + 1 --&gt; error<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc704\uc758 \ucf54\ub4dc\ub294 \ub0b4\ubd80\uc801\uc73c\ub85c \ub2e4\uc74c\uacfc \uac19\uc740 \ubc29\uc2dd\uc73c\ub85c \ucc98\ub9ac \ub429\ub2c8\ub2e4.<br>The above code is internally processed in the following way:<\/p>\n\n\n\n<p>\u2714\ufe0f self\ub294 struct ContentView: View {} \ub97c \uc758\ubbf8\ud569\ub2c8\ub2e4.<br>self refers to struct ContentView: View {}.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> self.countLet = 10\n self.countLetCount = self.countLet + 1<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uadf8\ub798\uc11c changeVarCount\ub098 countLetCount \ucd08\uae30\ud654\uc2dc self\uac00 \uc544\uc9c1 \uc644\uc804\ud788 \ucd08\uae30\ud654 \ub418\uc5c8\ub294\uc9c0 \ud655\uc2e4 \ud558\uc9c0 \uc54a\uae30 \ub54c\ubb38\uc5d0 \uc5d0\ub7ec\ub97c \ubc1c\uc0dd \uc2dc\ud0b5\ub2c8\ub2e4.<br>So when initializing changeVarCount or countLetCount, it raises an error because it is not sure if self is fully initialized yet.<\/p>\n\n\n\n<p>\u2714\ufe0f \uc704\uc758 \ubb38\uc81c\ub97c \ud574\uacb0\ud558\uae30 \uc704\ud574\uc11c\ub294 init(){}\uc548\uc5d0 \uac12\uc758 \ubcc0\uacbd\uacfc \ucd9c\ub825\uc744 \uc218\ud589\ud558\ub3c4\ub85d \ud569\ub2c8\ub2e4.<br>To solve the above problem, change the value and output it inside init(){}.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\uc640 \uc0c1\uc218\uc758 \ucd9c\ub825<br>Output of variables and constants<\/p>\n\n\n\n<p>\u2714\ufe0f\ud130\ubbf8\ub110 \ucd9c\ub825 :<br>terminal output<\/p>\n\n\n\n<p>&#8212; self \ucd08\uae30\ud654 \ubb38\uc81c\ub85c \uc778\ud574\uc11c init(){}\uc548\uc5d0 \uac12\uc744 \ucd9c\ub825 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Due to the self initialization issue, you can print the value inside init(){}.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    init() {\n        countLet = countVar + 1\n        print(\"countLet: \\(countLet)\") \/\/ \ucd9c\ub825 \/ print\n        \n        countVar2 = 10\n        countVar2 =  10 + 1\n        countLet2 = 20\n    }<\/code><\/pre>\n\n\n\n<p>&#8212; \uc571\uc758  VStack{}\uc758 \uc18d\uc131\uc778 onAppear(){}\ub97c \uc0ac\uc6a9\ud574\uc11c \ucd9c\ub825 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>You can output it using onAppear(){}, which is a property of the app&#8217;s VStack{}.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct ContentView: View {\n    var body: some View {\n        \n       VStack {\n         ..... code ....\n       }\n        .padding()\n        .onAppear{\n            print(\"countVar2:\\(countVar2)\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f\ubdf0\uc5d0 \ucd9c\ub825 :<br>output to view<\/p>\n\n\n\n<p>&#8212; \ubdf0\uc5d0\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \ubc29\uc2dd\uc73c\ub85c \ucd9c\ub825 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>The view can be output in the following ways:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \ubcc0\uc218\ub098 \uc0c1\uc218 \uac12\ub9cc \ucd9c\ub825 \/ Print only variable or constant values\nText(countVar)  \n\n\/\/ \ubcc0\uc218\ub098 \uc0c1\uc218\uac00 \ubb38\uc790\uc5f4\uacfc \ud568\uaed8 \ucd9c\ub825 \/ Print variables or constants with strings    \nText(\"Count Variable:\\(countVar)\") <\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/  \ubcc0\uc218\uc640 \uc0c1\uc218 \/ Variables and Constants\n\nimport SwiftUI\n\n\nstruct ContentView: View {\n    \n    \/\/ let :\n    \/\/ \uc0c1\uc218 \/ constant --&gt; \ucd08\uae30\ud654 \ud6c4 \uac12\uc744 \ubcc0\uacbd \ud560 \uc218 \uc5c6\uc74c.\n    \/\/ constant --&gt; The value cannot be changed after initialization.\n    \n    \/\/ var :\n    \/\/ \ubcc0\uc218 \/ variable --&gt; \ucd08\uae30\ud654 \ud6c4\uc5d0\ub3c4 \uac12\uc744 \ubc14\uafc0 \uc218 \uc788\uc74c.\n    \/\/ variable --&gt; The value can be changed even after initialization.\n    \n    \/\/ \ubaa8\ub4e0 \uc0ac\ud56d\uc774 \ucd08\uae30\ud654 \ub418\uc5c8\ub2e4\uace0 \ucef4\ud30c\uc77c\ub7ec\uac00 \ud310\ub2e8 \ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.\n    \/\/ \uadf8\ub798\uc11c Swift\ub294 \uc774\ub7f0 \ud615\ud0dc\uc758 \ucd08\uae30\ud654\ub97c \ub9c9\uace0 \uc788\uc2b5\ub2c8\ub2e4.\n    \n    \/\/ The compiler cannot determine that everything has been initialized.\n    \/\/ That's why Swift prevents this type of initialization.\n    \n    \/\/ var countVar = 10\n    \/\/ var countVar2 = 20\n    \/\/ var changeVarCount: Int = count + 1 --&gt; error\n    \n    \/\/ let countLet = 10\n    \/\/ let countLet2 = 20\n    \/\/ let countLetCount: Int = countLet + 1 --&gt; error\n    \n    \/*\n     let countLet = 10\n     let countLetCount = countLet + 1\n     \n     \ubc14\ub85c \uc704\uc758 \ud615\ud0dc\ub294 \ub0b4\ubd80\uc801\uc73c\ub85c \ub2e4\uc74c\uacfc \uac19\uc774 \ud574\uc11d\ub429\ub2c8\ub2e4.\n     The form just above is internally interpreted as follows:\n     \n     self.countLet = 10\n     self.countLetCount = self.countLet + 1\n\n     \uadf8\ub798\uc11c self(struct ContentView)\uac00 \uc544\uc9c1 \uc644\uc804\ud788 \ucd08\uae30\ud654 \ub418\uc5c8\ub294\uc9c0\n     \ubcf4\uc7a5 \ud560 \uc218 \uc5c6\uae30 \ub54c\ubb38\uc5d0 \uc624\ub958\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4.\n     \n     So, the error occurs because Swift can't guarantee that self(struct ContentView) is fully initialized.\n    *\/\n\n    \n    var countVar = 10\n    var countVar2: Int\n    \n    let countLet: Int\n    let countLet2:Int\n\n    \n    \/\/ init()\uc744 \uc0ac\uc6a9\ud558\uc5ec self\uac00 \uc0dd\uc131\ub41c \uc774\ud6c4 \ucd08\uae30\ud654 \ud558\uae30\n    \/\/ Initialize self after it is created using init()\n    \n    init() {\n        countLet = countVar + 1\n        print(\"countLet: \\(countLet)\")\n        \n        countVar2 = 10\n        countVar2 =  10 + 1\n        \n        countLet2 = 20\n        \n        \/\/ let\uc740 \uc0c1\uc218\uc774\uae30 \ub54c\ubb38\uc5d0 \uc544\ub798\uc758 \ucf54\ub4dc \uc2e4\ud589\uc2dc \ub2e4\uc74c\uacfc \uc624\ub958\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4.\n        \/\/ Since let is a constant, the following error occurs when executing the code below.\n        \/\/ countLet2 = 20 + 1\n        \n        \/\/ error: Immutable value 'self.countLet2' may only be initialized once\n\n\n    }\n    \n    \/\/ ====       body\uc548\uc5d0 \ubcc0\uc218 \uc120\uc5b8 \ud560 \uc218 \uc5c6\uc74c.       ==== \/\/\n    \/\/ ==== Cannot declare variables inside body ==== \/\/\n    \n    var body: some View {\n        \n        VStack {\n\n            \n            \/\/ ====        \ubdf0 \uc548\uc5d0\uc11c \ubcc0\uc218 \ucd9c\ub825 \ubc29\ubc95         ==== \/\/\n            \/\/ ==== How to output variables in a view ==== \/\/\n            \/\/ Text(countVar) --&gt; error\n            \n            Text(\"\\(countVar)\")\n            Text(\"Count Variable:\\(countVar)\")\n            Text(\"Count:\\(String(countVar))\")\n            \n        }\n        .padding()\n        .onAppear{\n            \n            \/\/ countLet\uc774 \ub450\ubc88\uc529 \ucd9c\ub825\ub418\ub294 \uc774\uc720\ub294\n            \/\/ \uac12 \ud0c0\uc785 \uad6c\uc870\uccb4(View)\ub77c\uc11c body\uac00 \uc7ac\uacc4\uc0b0\ub420 \ub54c\ub9c8\ub2e4 \uc0c8\ub85c\uc6b4 \uc778\uc2a4\ud134\uc2a4\uac00 \ub9cc\ub4e4\uc5b4\uc9d1\ub2c8\ub2e4.\n            \n            \/\/ The reason countLet is printed twice is because it is a value type structure (View),\n            \/\/ so a new instance is created each time body is recalculated.\n            \n            \n            \/\/onAppear\ub294 \ud654\uba74\uc5d0 \ub098\ud0c0\ub0a0\ub54c \ud55c\ubc88\ub9cc \uc2e4\ud589\ud558\ub294 \ucf54\ub4dc\ubd80\ubd84\uc785\ub2c8\ub2e4.\n            \/\/onAppear is a code section that runs only once when it appears on the screen.\n            \n            print(\"countVar2:\\(countVar2)\")\n        }\n    }\n}\n\n#Preview {\n    ContentView()\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc571\uc2e4\ud589\/Run App<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\ubcc0\uc218\uc640 \uc0c1\uc218 \/ Const and Variables\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/wPDsK1hsgtI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \ubcc0\uc218\uc640 \uc0c1\uc218\ub97c \uc0ac\uc6a9\ud574\uc11c \ud654\uba74\uacfc \ud130\ubbf8\ub110\uc5d0 \uac12\uc744 \ucd9c\ub825\ud558\ub294 \uc571\uc785\ub2c8\ub2e4.Below is an app that uses variables and constants to output values \u200b\u200bto the screen and terminal. \ud83d\udc49\ud83c\udffb \ubcc0\uc218\ub780 \uc5b4\ub5a4 \uac12\uc744 \ub2f4\ub294 \uadf8\ub987\uacfc \uac19\uc73c\uba70 \ucd5c\ucd08\uc5d0 \uac12\uc774 \ud560\ub2f9\ub41c \ud6c4(\ucd08\uae30\ud654\ub77c\uace0 \ud568) \uac12\uc758 \ubcc0\uacbd\uc774 \uac00\ub2a5\uc740 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.A variable is like a container that holds a value, and it means [&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-2854","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\/2854","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=2854"}],"version-history":[{"count":23,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2854\/revisions"}],"predecessor-version":[{"id":3007,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2854\/revisions\/3007"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}