{"id":4126,"date":"2026-01-23T14:30:42","date_gmt":"2026-01-23T05:30:42","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=4126"},"modified":"2026-01-23T15:49:05","modified_gmt":"2026-01-23T06:49:05","slug":"swift-struct-and-class","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/01\/23\/swift-struct-and-class\/","title":{"rendered":"[Swift]\uad6c\uc870\uccb4\uc640 \ud074\ub798\uc2a4\/Struct and Class"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb Swift\uc5d0\uc11c\ub294 \uad6c\uc870\uccb4\ub97c \uc911\uc694\ud558\uac8c \ub2e4\ub8f9\ub2c8\ub2e4. <br>In Swift, structures are treated with great importance.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Swift\ub97c \uc0ac\uc6a9\ud574\ubcf4\uba74  struct\ub97c \ud074\ub798\uc2a4\ubcf4\ub2e4 \ub354 \ub9ce\uc774 \uc0ac\uc6a9\ud558\ub294\uac83\uc744 \ubcfc \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>If you use Swift, you will see that structs are used more than classes.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb struct\uc640 class\ub294 \ubb38\ubc95\ud615\uc2dd\uc740 \uac70\uc758 \ube44\uc2b7\ud558\uc9c0\ub9cc struct\ub294 \uc778\uc2a4\ud134\uc2a4\ub97c \ubcf5\uc0ac\ud558\uace0 class\ub294 \uc778\uc2a4\ud134\uc2a4\ub97c \ucc38\uc870\ud569\ub2c8\ub2e4.<br>struct and class have almost the same grammar, but struct copies instances and class references instances.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\uc758 \ucf54\ub4dc\uc5d0\uc11c \uad6c\uc870\uccb4 \ubaa8\ub378\uc758 \ubcf5\uc0ac\ubcf8 \uc778\uc2a4\ud134\uc2a4 name(user3.name)\uc744 \ubc14\uafd4\ub3c4 \uc6d0\ubcf8 \uc778\uc2a4\ud134\uc2a4\uc758 name(user.name)\uac12\uc774 \ubc14\ub00c\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br>In the code below, changing the name(user3.name) of the copy instance of the struct model does not change the name(user.name) value of the original instance.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ud074\ub798\uc2a4 \ubaa8\ub378\uc758 \uacbd\uc6b0 \ucd08\uae30 \uac12\uc774 &#8220;God of war&#8221;\uc778\ub370 \ubcf5\uc0ac\ubcf8 \uc778\uc2a4\ud134\uc2a4\uc758 gametitle\uc744 \ubcc0\uacbd\ud558\uba74 &#8220;God of war ragnarok&#8221;\ub85c \uc6d0\ubcf8 \uc778\uc2a4\ud134\uc2a4 \uac12\uc774 \ubcc0\uacbd\ub418\ub294 \uac83\uc744 \ud655\uc778 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. <br>For the class model, the initial value is &#8220;God of war&#8221;, but if you change the gametitle of the copy instance, you can see that the value of the original instance changes to &#8220;God of war ragnarok&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    init(user: User = User(name: \"Atreus\", age: 17)) {\n        \/\/ \uad6c\uc870\uccb4 \uc6d0\ubcf8\uc778\uc2a4\ud134\uc2a4 \/ Struct Original Instance\n        user2 = user\n        \n        \/\/ \uad6c\uc870\uccb4 \uc0ac\ubcf8\uc778\uc2a4\ud134\uc2a4 \/ struct copy instance\n        user3 = user2\n        user3.name = \"Kratos\"\n        \n        \/\/ \ud074\ub798\uc2a4 \uc6d0\ubcf8 \uc778\uc2a4\ud134\uc2a4 \/ class original instance\n        game = Game(title:\"God of war\")\n        gameTitleChange = game\n        \n        \/\/ \ud074\ub798\uc2a4 \ubcf5\uc0ac\ubcf8 \uc778\uc2a4\ud134\uc2a4 \/ class copy instance\n        gameTitleChange.gametitle = \"God of war ragnarok\"\n    }<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\uc804\uccb4 \ucf54\ub4dc \/ Full Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport SwiftUI\n\n\/\/  \uad6c\uc870\uccb4 \ubaa8\ub378 \/ struct model\nstruct User {\n    var name: String\n    var age: Int\n}\n\n\/\/ \ud074\ub798\uc2a4 \ubaa8\ub378 \/ class model\nclass Game {\n    var gametitle:String\n\n    init(title:String){\n        self.gametitle = title\n    }\n}\n\nstruct ContentView: View {\n    \n    \/\/ \uad6c\uc870\uccb4\uc6a9 \/ For struct\n    @State private var user = User(name: \"Kratos\", age: 650)\n    var user2: User\n    var user3: User\n    let apptitle = \"Struct Example\"\n    \n    \/\/ \ud074\ub798\uc2a4\uc6a9 \/ For class\n    let game:Game\n    let gameTitleChange:Game\n    \n    init(user: User = User(name: \"Atreus\", age: 17)) {\n        \/\/ \uad6c\uc870\uccb4 \uc6d0\ubcf8\uc778\uc2a4\ud134\uc2a4 \/ Struct Original Instance\n        user2 = user\n        \n        \/\/ \uad6c\uc870\uccb4 \uc0ac\ubcf8\uc778\uc2a4\ud134\uc2a4 \/ struct copy instance\n        user3 = user2\n        user3.name = \"Kratos\"\n        \n        \/\/ \ud074\ub798\uc2a4 \uc6d0\ubcf8 \uc778\uc2a4\ud134\uc2a4 \/ class original instance\n        game = Game(title:\"God of war\")\n        gameTitleChange = game\n        \n        \/\/ \ud074\ub798\uc2a4 \ubcf5\uc0ac\ubcf8 \uc778\uc2a4\ud134\uc2a4 \/ class copy instance\n        gameTitleChange.gametitle = \"God of war ragnarok\"\n    }\n    \n    var body: some View {\n        VStack(spacing: 20) {\n            Image(systemName: \"person.circle.fill\")\n                .imageScale(.large)\n                .foregroundStyle(.tint)\n            \n            \/\/ \uc571 \ud0c0\uc774\ud2c0 \/ app title\n            Text(\"\\(apptitle)\")\n                .font(.largeTitle)\n            \n            \/\/ \uac8c\uc784 \ud0c0\uc774\ud2c0,\ud074\ub798\uc2a4,\uc6d0\ubcf8 \uac12\uc774 \ubcc0\uacbd\ub428\n            \/\/ Game title, class, and original values \u200b\u200bhave changed\n            Text(\"\uac8c\uc784 \ud0c0\uc774\ud2c0\/gmae title: \\(game.gametitle)\")\n            \n            \/\/  \uad6c\uc870\uccb4 \uac12 \uc0ac\uc6a9\n            \/\/ Using structure values\n            Text(\"\ubd80\ubaa8\ub2d8 \uc774\ub984\/Parent Name: \\(user.name)\")\n            Text(\"\ub098\uc774\/Age: \\(user.age)\")\n            Text(\"\uc544\ub4e4 \uc774\ub984\/son name: \\(user2.name)\")\n            Text(\"\uc544\ub4e4 \ub098\uc774\/son age: \\(user2.age)\")\n\n            \/\/  \uad6c\uc870\uccb4 \uac12 \ubcc0\uacbd\n            \/\/ Change structure value\n            Button(\"\uc544\ubc84\uc9c0 \ub098\uc774 \uc99d\uac00\/father's age increases\") {\n                user.age += 1\n            }\n            Button(\"\uc544\ubc84\uc9c0 \ub098\uc774 \uac10\uc18c\/father's age decreases\") {\n                user.age -= 1\n            }\n        }\n        .padding()\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-full\"><img loading=\"lazy\" decoding=\"async\" width=\"490\" height=\"1014\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/structExample.png\" alt=\"\" class=\"wp-image-4136\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/structExample.png 490w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/structExample-145x300.png 145w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/01\/structExample-400x828.png 400w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb Swift\uc5d0\uc11c\ub294 \uad6c\uc870\uccb4\ub97c \uc911\uc694\ud558\uac8c \ub2e4\ub8f9\ub2c8\ub2e4. In Swift, structures are treated with great importance. \ud83d\udc49\ud83c\udffb Swift\ub97c \uc0ac\uc6a9\ud574\ubcf4\uba74 struct\ub97c \ud074\ub798\uc2a4\ubcf4\ub2e4 \ub354 \ub9ce\uc774 \uc0ac\uc6a9\ud558\ub294\uac83\uc744 \ubcfc \uc218 \uc788\uc2b5\ub2c8\ub2e4.If you use Swift, you will see that structs are used more than classes. \ud83d\udc49\ud83c\udffb struct\uc640 class\ub294 \ubb38\ubc95\ud615\uc2dd\uc740 \uac70\uc758 \ube44\uc2b7\ud558\uc9c0\ub9cc struct\ub294 \uc778\uc2a4\ud134\uc2a4\ub97c \ubcf5\uc0ac\ud558\uace0 class\ub294 \uc778\uc2a4\ud134\uc2a4\ub97c \ucc38\uc870\ud569\ub2c8\ub2e4.struct and class have almost the same [&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-4126","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\/4126","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=4126"}],"version-history":[{"count":10,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/4126\/revisions"}],"predecessor-version":[{"id":4137,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/4126\/revisions\/4137"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=4126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=4126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=4126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}