Avatar
@Published@StateObject ( @ObservedObject ) の projectedValue の違い import SwiftUI import Combine final class UserViewModel: ObservableObject { @Published var user: User = .init(name: "Swift", age: 8) } //let viewModel: UserViewModel = .init() // //viewModel.$user // Publisher<User, Never> struct UserView: View { // var _user: State<User> @StateObject var viewModel: UserViewModel = .init() // @State private var user: User = .init(name: "Swift", age: 8) // @ViewBuilder var body: some View { VStack { Text(viewModel.user.name) .font(.title) AgeCounterView(age: $viewModel // ObservedObject<UserViewModel>.Wrapper .user // Binding<User> .age // Binding<Int> ) } } } struct AgeCounterView: View { @Binding var age: Int init(age: Binding<Int>) { self._age = age } var body: some View { Text("age: \(age)") Button("Next Year") { age += 1 } } } struct UserView_Previews: PreviewProvider { static var previews: some View { UserView() } } (edited)