stateobject (2) 썸네일형 리스트형 SwiftUI] StateObject, Published, ObservableObject https://www.hackingwithswift.com/books/ios-swiftui/sharing-swiftui-state-with-stateobject swiftui 에서 struct 인스턴스 변수는 간단한 @State 로 (내부 프로퍼티의) 변경을 감지하여 UI에 반영할 수 있다. 하지만 class 인스턴스 변수는 @State 를 붙여도 내부 프로퍼티의 변경을 감지하지 못한다. class 의 내부 프로퍼티의 변경을 감지하도록 하는 방법은 다음과 같다. 1. 외부에서 변경을 감지하려는 클래스의 프로퍼티에 @Published 를 붙여준다. class User { @Published var name: String = "Micky" @Published var age: Int = 10 } 2. view.. [SwiftUI] @State, struct, class https://www.hackingwithswift.com/books/ios-swiftui/why-state-only-works-with-structs why state only works with structs 라는 제목의 강의. swiftui 에서 변경될 수 있는 변수에 @State 를 붙이는 것에 익숙해졌다. 이 @State 변수에는 스위프트의 기본 형 뿐만 아니라, 사용자형도 사용할 수 있다. 다음과 같다. struct User { var firstName = "Bilbo" var lastName = "Baggins" } struct ContentView: View { @State private var user = User() var body: some View { VStack { VStack {.. 이전 1 다음