{"id":5792,"date":"2026-05-29T11:07:00","date_gmt":"2026-05-29T02:07:00","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5792"},"modified":"2026-05-29T11:06:42","modified_gmt":"2026-05-29T02:06:42","slug":"swift-observedobject-and-stateobject-1","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/29\/swift-observedobject-and-stateobject-1\/","title":{"rendered":"[Swift]ObservedObject and StateObject &#8211; 1"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc5d0\uc11c ObservedObject\uc640 StateObject\ub97c \uc0ac\uc6a9\ud558\ub294 \uac04\ub2e8\ud55c \ubc29\ubc95\uc785\ub2c8\ub2e4.<br>This is a simple way to use ObservedObject and StateObject in Swift.<\/p>\n\n\n\n<p>\u2714\ufe0f <code>@StateObject<\/code>\uc640\u00a0<code>@ObservedObject<\/code>\ub294 \ub458\ub2e4 \ubdf0\uc5d0 \ubc14\uc778\ub529\ud558\ub294 \ubc29\ubc95\uc774\uc9c0\ub9cc, \ub458\uc740 \uc57d\uac04\uc758 \ucc28\uc774\uac00 \uc788\uc2b5\ub2c8\ub2e4.<br>Both @StateObject and @ObservedObject are methods for binding to a view, but there are slight differences between the two.<\/p>\n\n\n\n<p>\u2714\ufe0f <strong><strong>@StateObject<\/strong><\/strong><\/p>\n\n\n\n<p>&#8212; \uc77c\ubc18\uc801\uc73c\ub85c \ubdf0\uc5d0\uc11c \uc9c1\uc811 \uc0c1\ud0dc \uac1d\uccb4\ub97c \uc0dd\uc131\ud558\uace0 \uad00\ub9ac\ud560 \ub54c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>It is generally used when creating and managing state objects directly in the view.<\/p>\n\n\n\n<p>&#8212; \ubdf0\uac00 \ucc98\uc74c \uc0dd\uc131\ub420 \ub54c\ub9cc \ud574\ub2f9 ObservableObject \uc778\uc2a4\ud134\uc2a4\ub97c \ucd08\uae30\ud654\ud558\uace0, \ubdf0\uac00 \ub2e4\uc2dc \ub80c\ub354\ub9c1\ub418\uac70\ub098 \uac31\uc2e0\ub420 \ub54c\ub294 \uae30\uc874 \uc778\uc2a4\ud134\uc2a4\ub97c \uc720\uc9c0\ud569\ub2c8\ub2e4.<br>Initialize the ObservableObject instance only when the view is first created, and maintain the existing instance when the view is re-rendered or updated.<\/p>\n\n\n\n<p>&#8212; \ubdf0\uc5d0\uc11c \uc9c1\uc811 \uc0c1\ud0dc \uad00\ub9ac\ub97c \ud560\ub54c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>It is used when managing state directly in the view.<\/p>\n\n\n\n<p>&#8212; \uc544\ub798\uc758 \ucf54\ub4dc\uac00 \uc774 \ubd80\ubd84\uc5d0 \ud574\ub2f9\ud569\ub2c8\ub2e4.<br>The code below corresponds to this part.<\/p>\n\n\n\n<p>\u2714\ufe0f <strong>@ObservedObject<\/strong><\/p>\n\n\n\n<p>&#8212; \uc678\ubd80\uc5d0\uc11c \uc774\ubbf8 \uc0dd\uc131\ub41c ObservableObject\ub97c \uc804\ub2ec\ubc1b\uc544 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>It receives and uses an ObservableObject that has already been created externally.<\/p>\n\n\n\n<p>&#8212; \ubdf0\uac00 \ub2e4\uc2dc \uc0dd\uc131\ub420 \ub54c\ub9c8\ub2e4 ObservableObject \uc778\uc2a4\ud134\uc2a4\uac00 \uacc4\uc18d \uc0c8\ub85c \uc0dd\uc131\ub418\ub294 \uacbd\uc6b0\uac00 \uc788\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>There may be cases where new ObservableObject instances are created every time the view is recreated.<\/p>\n\n\n\n<p>&#8212; \uadf8\ub798\uc11c \ubd80\ubaa8\ubdf0\ub97c \uc790\uc2dd\ubdf0\uc5d0 \uc804\ub2ec\ud560\ub54c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>So, it is used when passing a parent view to a child view.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport SwiftUI\nimport Combine\n\n\nclass UserSettings: ObservableObject {\n  @Published var username = \"Anonymous\"\n}\n\nstruct ContentView: View {\n    \n    \/\/ @StateObject\ub294 SwiftUI\uc758 state storage(\ubdf0\uc758 \ub77c\uc774\ud504 \uc2f8\uc774\ud074 \ub3d9\uc548 \ubcf4\uad00\ub418\ub294 \uc800\uc7a5\uc18c)\uc5d0\n    \/\/ \uc778\uc2a4\ud134\uc2a4\ub97c \uc800\uc7a5\ud558\uace0, \ucc98\uc74c \ubdf0\uac00 \uc0dd\uc131\ub420 \ub54c\ub9cc \ucd08\uae30\ud654\ud558\ub3c4\ub85d \ubcf4\uc7a5\ud569\ub2c8\ub2e4.\n    \/\/ @StateObject stores an instance in SwiftUI's state storage (which is kept throughout the view's lifecycle) and ensures that it is initialized only when the view is first created.\n    \n    @StateObject var settings = UserSettings()\n    \n    \/\/ @ObservedObject\ub294 \uad6c\uc870\uccb4\uc758 \ucd08\uae30\ud654 \uc2dc\uc810\uc5d0 \uacc4\uc18d\ud574\uc11cUserSettings()\ub97c \ub2e4\uc2dc \uc2e4\ud589 \ud569\ub2c8\ub2e4.\n    \/\/ @ObservedObject will continually re-run UserSettings() at the time of initialization of the struct.\n    \n    \/\/@ObservedObject var settings = UserSettings()\n    \n    var body: some View {\n        VStack {\n            Image(systemName: \"globe\")\n                .imageScale(.large)\n            \n                .foregroundStyle(.tint)\n            Text(\"User name:\\(settings.username)\")\n                .padding()\n            \n            Button(\"John Doe\"){\n                settings.username = \"Jhon Doe\"\n            }\n            Button(\"Anonymous\"){\n                settings.username = \"Anonymous\"\n            }\n            \n            Text(\"Child View\")\n        }\n        .padding()\n    }\n}\n\n\n#Preview {\n    ContentView()\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2a4\ud06c\ub9b0 \uc0f7 \/ ScreenShot<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"424\" height=\"884\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/05\/object.png\" alt=\"\" class=\"wp-image-5804\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/05\/object.png 424w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/05\/object-144x300.png 144w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/05\/object-400x834.png 400w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc5d0\uc11c ObservedObject\uc640 StateObject\ub97c \uc0ac\uc6a9\ud558\ub294 \uac04\ub2e8\ud55c \ubc29\ubc95\uc785\ub2c8\ub2e4.This is a simple way to use ObservedObject and StateObject in Swift. \u2714\ufe0f @StateObject\uc640\u00a0@ObservedObject\ub294 \ub458\ub2e4 \ubdf0\uc5d0 \ubc14\uc778\ub529\ud558\ub294 \ubc29\ubc95\uc774\uc9c0\ub9cc, \ub458\uc740 \uc57d\uac04\uc758 \ucc28\uc774\uac00 \uc788\uc2b5\ub2c8\ub2e4.Both @StateObject and @ObservedObject are methods for binding to a view, but there are slight differences between the two. \u2714\ufe0f @StateObject &#8212; \uc77c\ubc18\uc801\uc73c\ub85c \ubdf0\uc5d0\uc11c \uc9c1\uc811 \uc0c1\ud0dc \uac1d\uccb4\ub97c \uc0dd\uc131\ud558\uace0 [&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-5792","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\/5792","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=5792"}],"version-history":[{"count":12,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5792\/revisions"}],"predecessor-version":[{"id":5805,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5792\/revisions\/5805"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}