{"id":2825,"date":"2025-11-24T13:11:05","date_gmt":"2025-11-24T04:11:05","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2825"},"modified":"2025-11-24T20:47:28","modified_gmt":"2025-11-24T11:47:28","slug":"swift-sharing-ui-state-and-variables","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/11\/24\/swift-sharing-ui-state-and-variables\/","title":{"rendered":"[Swift]UI\uc0c1\ud0dc\uc640 \ubcc0\uc218\ub97c \uacf5\uc720\ud558\uae30 \/ Sharing UI state and variables"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \ud504\ub85c\uc81d\ud2b8 \uba85\uc774 PublisherSubscribe\uc778 \uacbd\uc6b0 \uc785\ub2c8\ub2e4.<br>If the project name is PublisherSubscribe.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb SwiftUI\uc5d0\uc11c @Publisher, @StateObject ,@ObservedObject\ub97c \ud1b5\ud574\uc11c \ubcc0\uc218\ub098 \uc0c1\ud0dc\ub97c \uacf5\uc720\ud569\ub2c8\ub2e4.<br>In SwiftUI, variables and states are shared through @Publisher, @StateObject, and @ObservedObject.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffbSwiftUI\uc5d0\uc11c \ubdf0\uc758 \uac1c\ub150\uc740 struct {} \uad6c\uc870\ub85c\uc368 \ud654\uba74\uc5d0 \ubcf4\uc5ec\uc9c0\ub294 \uae30\ub2a5\uc744 \uac00\uc9c0\uace0 \uc788\ub294 \uac83\uc785\ub2c8\ub2e4.<br>In SwiftUI, the concept of a view is a struct {} structure that has the functionality to be displayed on the screen.<\/p>\n\n\n\n<p>\u2714\ufe0f@Publisher : <br>ObservableObject \uc548\uc5d0\uc11c \uc0ac\uc6a9\ud558\ub294 property wrapper\uc785\ub2c8\ub2e4.<br>View\uc5d0\uc11c @ObservedObject\ub098 @StateObject\ub85c \uc774 ViewModel\uc744 \uad00\ucc30\ud558\uba74 UI\uac00 \uc790\ub3d9 \uac31\uc2e0\ub429\ub2c8\ub2e4.<\/p>\n\n\n\n<p>This is a property wrapper used within ObservableObject. When the View observes this ViewModel using @ObservedObject or @StateObject, the UI is automatically updated.<\/p>\n\n\n\n<p>\u2714\ufe0f @StateObject :<br>ViewModel \uac19\uc740 \ucc38\uc870 \ud0c0\uc785 \uc0c1\ud0dc\ub97c View \uc7ac\uc0dd\uc131\uc5d0\ub3c4 \uc720\uc9c0\ud569\ub2c8\ub2e4.<br>Maintains reference type state, such as ViewModel, even when View is recreated.<\/p>\n\n\n\n<p>\uadf8\ub7ec\uba74 View\ub294 \uc5b8\uc81c \uc7ac\uc0dd\uc131 \ub418\ub098?<br>So when is the View regenerated?<\/p>\n\n\n\n<p>1)State\ub098 Binding\uc774 \ubc14\ub014 \ub54c \/ When State or Binding changes<\/p>\n\n\n\n<p>2)\ubd80\ubaa8 View\uac00 \uc5c5\ub370\uc774\ud2b8 \ub420 \ub54c \/ When the parent View is updated<\/p>\n\n\n\n<p>3)\ud654\uba74 rotation, layout change \ub4f1\uc774 \ubc1c\uc0dd\ud560 \ub54c \/ When screen rotation, layout change, etc. occur<\/p>\n\n\n\n<p>\u2714\ufe0f@ObservedObject : <br>\uc678\ubd80\uc5d0\uc11c \uc804\ub2ec\ubc1b\uc544 \uad00\ucc30\ub9cc, \uc7ac\uc0dd\uc131 \uc2dc \uac1d\uccb4 \uc18c\uc720\uad8c \uc5c6\uc2b5\ub2c8\ub2e4.<br>Observe only when received from outside, no ownership of object when recreated.<\/p>\n\n\n\n<p>\u2714\ufe0fObservableObject\ub294 ViewModel\uc774 UI\uc5d0\uac8c \u201c\ub0b4\uac00 \ubcc0\ud588\uc73c\ub2c8 \ub2e4\uc2dc \uadf8\ub824\ub2ec\ub77c\u201d\ub77c\uace0 \uc54c\ub9b4 \uc218 \uc788\uac8c \ud558\ub294 SwiftUI \uc804\uc6a9 \ud504\ub85c\ud1a0\ucf5c\uc785\ub2c8\ub2e4.<br>ObservableObject is a SwiftUI-specific protocol that allows a ViewModel to tell the UI, \u201cI changed, so redraw me.\u201d<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc774 \uc571\uc740 \ubc84\ud2bc\uc744 \ub204\ub97c \uacbd\uc6b0 \ubd80\ubaa8 \ubdf0\uc640 \uc790\uc2dd \ubdf0\uc5d0\uc11c \uce74\uc6b4\ud130 \uac12\uc774 \ub3d9\uc77c\ud558\uac8c \uc99d\uac00\ud558\ub294 \uc571\uc785\ub2c8\ub2e4.<br>This app is one that increases the counter value equally in both the parent view and child view when a button is pressed.<\/p>\n\n\n\n<p><strong>2.\ucf54\ub4dc\/Code<\/strong><\/p>\n\n\n\n<p>\u2714\ufe0f PublisherSubscribeApp.swift<\/p>\n\n\n\n<p>\ubcf4\ud1b5 \uc774 \ud30c\uc77c\uc740 \ucc38\uc870\uc6a9\uc785\ub2c8\ub2e4. \uc790\ub3d9\uc0dd\uc131\ub418\uae30\ub54c\ubb38\uc5d0 \uc218\uc815\ud558\uc9c0 \uc54a\uc544\ub3c4 \ub429\ub2c8\ub2e4.<br>This file is usually for reference only. It is automatically generated and does not need to be modified.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\n@main\nstruct PublisherSubscribeApp: App {\n    var body: some Scene {\n        WindowGroup {\n            ContentView()\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f ContentView.swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\nstruct ContentView: View {\n    var body: some View {\n        VStack {\n            Image(systemName: \"globe\")\n                .imageScale(.large)\n                .foregroundStyle(.tint)\n            Text(\"Hello, world!\")\n            \n            ParentView()\n        }\n        .padding()\n    }\n}\n\n#Preview {\n    ContentView()\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f ParentView.swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\nstruct ParentView: View {\n    \n    \/\/ @StateObject \u2192 ViewModel\uc744 \"\uc774 View\uc5d0\uc11c \uc0dd\uc131\ud558\uace0 \ubcf4\uc720\"\ud568\n    \/\/ ViewModel\uc744 \uc9c1\uc811 \uc0ac\uc6a9\ud560 \uacbd\uc6b0 @StateObject\uc0ac\uc6a9 \ud568\n    \n    \/\/ @StateObject \u2192 \"Create and hold the ViewModel in this View\"\n    \/\/ Use @StateObject when using the ViewModel directly.\n    \n    @StateObject var viewModel = CounterViewModel()\n    \n    var body: some View {\n        VStack(spacing: 20) {\n            Text(\"Parent View Count: \\(viewModel.count)\")\n                .font(.title)\n            \n            Button(\"Increase\") {\n                viewModel.increase()\n            }\n            \n            \/\/ \uc790\uc2dd\uc5d0\uac8c ViewModel \uc804\ub2ec\n            ChildView(viewModel: viewModel)\n        }\n        .padding()\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f ChildView.swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\n\/\/ ParentView\uc5d0 \ud3ec\ud568\ub418\ub294 \ubdf0\nstruct ChildView: View {\n    \/\/ @ObservedObject \u2192 \ubd80\ubaa8\uc5d0\uc11c \uc804\ub2ec\ubc1b\uc740 ViewModel\uc744 \"\uad00\ucc30\"\ub9cc \ud568\n    \/\/ \uc790\uc2dd \ubdf0\uac00 \ubd80\ubaa8 \ubdf0\uc5d0\uac8c\uc11c ViewModel\uc744 \uc804\ub2ec \ubc1b\uc740\uacbd\uc6b0 @ObservedObject\ub97c \uc0ac\uc6a9\ud568.\n    @ObservedObject var viewModel: CounterViewModel\n    \n    var body: some View {\n        VStack(spacing: 10) {\n            Text(\"Child View Count: \\(viewModel.count)\")\n                .font(.headline)\n            \n            Button(\"Increase from Child\") {\n                viewModel.increase()\n            }\n        }\n        .padding()\n        .border(Color.gray)\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f CounterViewModel<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Foundation\nimport Combine\n\n\/\/ ObservableObject\ub294 ViewModel\uc774 UI\uc5d0\uac8c \u201c\ub0b4\uac00 \ubcc0\ud588\uc73c\ub2c8 \ub2e4\uc2dc \uadf8\ub824\ub2ec\ub77c\u201d\ub77c\uace0 \uc54c\ub9b4 \uc218 \uc788\uac8c \ud558\ub294 SwiftUI \uc804\uc6a9 \ud504\ub85c\ud1a0\ucf5c\uc785\ub2c8\ub2e4.\n\/\/ SwiftUI\uc5d0\uc11c ObservableObject\ub294 **\u201c\uc678\ubd80\uc5d0\uc11c \uad00\ucc30 \uac00\ub2a5\ud55c \uac1d\uccb4\u201d**\ub97c \ub9cc\ub4e4\uae30 \uc704\ud55c \ud504\ub85c\ud1a0\ucf5c\uc785\ub2c8\ub2e4.\n\n\/\/ ObservableObject is a SwiftUI-specific protocol that allows the ViewModel to tell the UI, \"I changed, so please redraw me.\"\n\/\/ In SwiftUI, ObservableObject is a protocol for creating \"objects that are observable from the outside\".\n\nclass CounterViewModel: ObservableObject {\n    \n    \/\/ @Published: \uac12\uc774 \ubc14\ub00c\uba74 \uc774\ubca4\ud2b8\ub97c \ub0b4\ubcf4\ub0b4\ub294 Publisher\n    \/\/ @Published: Publisher that emits an event when the value changes\n    \n    @Published var count: Int = 0\n    \n    func increase() {\n        count += 1\n    }\n}\n\n\/*\n \n @Published = objectWillChange.send()\ub97c \uc790\ub3d9\uc73c\ub85c \ud638\ucd9c\ud574\uc8fc\ub294 \ud3b8\uc758 \uae30\ub2a5\uc785\ub2c8\ub2e4.\n \uc6d0\ub798\ub294 \uc9c1\uc811 objectWillChange.send()\ub97c \ud638\ucd9c\ud558\uba74, @Published \uc5c6\uc774\ub3c4 UI\ub97c \uc5c5\ub370\uc774\ud2b8 \uac00\ub2a5\ud569\ub2c8\ub2e4.\n \ub2e8, \uac1c\ubc1c\uc790\uac00 \uac12\uc774 \ubc14\ub014 \ub54c\ub9c8\ub2e4 send()\ub97c \uc9c1\uc811 \ud638\ucd9c\ud574\uc57c \ud569\ub2c8\ub2e4.\n \uc774\ub7f0 \ubd80\ubd84\uc744 \uc790\ub3d9\uc73c\ub85c \ucc98\ub9ac\ud574\uc8fc\ub294\uac8c @Published\uc785\ub2c8\ub2e4.\n \n @Published is a convenience feature that automatically calls objectWillChange.send().\n Originally, if you call objectWillChange.send() directly, you can update the UI without @Published .\n However, developers must manually call send() every time a value changes.\n @Published handles this automatically.\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-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=\"[Swift]UI\uc0c1\ud0dc\uc640 \ubcc0\uc218\ub97c \uacf5\uc720\ud558\uae30 \/ Sharing UI state and variables\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/J2BpJYR3Qf0?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 \ud504\ub85c\uc81d\ud2b8 \uba85\uc774 PublisherSubscribe\uc778 \uacbd\uc6b0 \uc785\ub2c8\ub2e4.If the project name is PublisherSubscribe. \ud83d\udc49\ud83c\udffb SwiftUI\uc5d0\uc11c @Publisher, @StateObject ,@ObservedObject\ub97c \ud1b5\ud574\uc11c \ubcc0\uc218\ub098 \uc0c1\ud0dc\ub97c \uacf5\uc720\ud569\ub2c8\ub2e4.In SwiftUI, variables and states are shared through @Publisher, @StateObject, and @ObservedObject. \ud83d\udc49\ud83c\udffbSwiftUI\uc5d0\uc11c \ubdf0\uc758 \uac1c\ub150\uc740 struct {} \uad6c\uc870\ub85c\uc368 \ud654\uba74\uc5d0 \ubcf4\uc5ec\uc9c0\ub294 \uae30\ub2a5\uc744 \uac00\uc9c0\uace0 \uc788\ub294 \uac83\uc785\ub2c8\ub2e4.In SwiftUI, the concept of a view is a struct {} structure that [&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-2825","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\/2825","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=2825"}],"version-history":[{"count":13,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2825\/revisions"}],"predecessor-version":[{"id":2853,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2825\/revisions\/2853"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}