Demo.swift
ContentView.swift
App.swift
import SwiftUI
class CounterModel: ObservableObject {
@Published var count = 0
func increment() { count += 1 }
}
struct ChildView: View {
@ObservedObject var model: CounterModel
var body: some View {
HStack {
Text("Count: \(model.count)")
Button("Inc") { model.increment() }
}
}
}
struct ParentView: View {
@StateObject private var model = CounterModel()
var body: some View {
VStack(spacing: 12) {
ChildView(model: model)
}
.padding()
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
ParentView()
}
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}