Chapter 3. State and data flow
Driving changes in your UI with state and bindings
- 하나의 뷰 안에서 데이터 의존관계를
State
로 나타낸다. 그리고 다른 뷰에 Binding
을 통해 이러한 의존 관계를 알린다.
Section 1. Separate properties and imperative code from the view
- 뷰가 둘 이상의 State 데이터를 관리해야하는 경우에, 그 데이터를 분리된 뷰(구조체)로 관리하는 것이 좋다.
- 프로퍼티와 필수 코드를 뷰의 바깥으로 옮김으로써 선언적 뷰 인터페이스 코드를 더 읽기 쉬워진다. 또한 State 변화 유닛 테스트를 구현하는 것이 쉬워진다.
- 샘플 앱에서는 RecipeEditorConfig라는 구조체를 정의함으로써 imperactive code, State 데이터를 분리한다.
== 이하 내용 참고: https://developer.apple.com/tutorials/swiftui-concepts/driving-changes-in-your-ui-with-state-and-bindings
Section 2. Bind the view to its state data
- ReceipeEditor 뷰 내부의 변화를 위해서 ReceipeEditorConfig의 프로퍼티(State data)와 메소드가 필요하다.
- RecepeEditor
- @Binding 프로퍼티 래퍼를 사용하여 ReceipeEditorConfig 객체를 생성한다.
- Binding은 읽고 쓰는 양방향 데이터 바인딩을 지원한다.
Section 3. Create a state variable in another view
잔잔바리 공부
mutating 키워드
- Swift에서 class는 레퍼런스 타입이고, struct와 열거형은 값 타입이다.
- 값 타입의 속성(구조체, 열거형)은 메소드에서 프로퍼티를 수정할 수 없다.
- 그러나
mutating
키워드를 붙인 메소드에서는 프로퍼티를 수정할 수 있다.