Search
Duplicate

State 상태 변수

상태
완료
A property wrapper type that can read and write a value managed by SwiftUI.
Declaration
@frozen @propertyWrapper struct State<Value>
Swift
복사

Overview

State value 는 값이 변경될때마다 view 는 현재 외관을 무효화한 뒤, 자신의 body 를 다시 계산한다.
변수의 값이 변경되면 즉시 사용자에게 변경된 값에 의한 뷰를 제공해야할 경우, 또는 뷰의 전환에 주로 사용된다
State instance isn’t the value itself; it’s a means of reading and writing the value. To access a state’s underlying value, use its variable name, which returns the wrappedValue property value.
State 객체는 그 자체로는 값이 아니다. value 를 읽어오고 쓰는 수단이다.
값에 접근하려면 변수의 이름을 통해 접근한다 > 그리고 이는 wrapped value 를 리턴한다.

주의사항

State 프로퍼티는 반드시 뷰의 body 안에서만 접근 해야한다.
또는 뷰의 body 내부의 메서드에서만 호출 되어야 한다.
State 프로퍼티를 private 로 선언하자. 자칫하면 고객이 이값에 접근할 수 있다.
계층구조내의 또다른 뷰에 State 프로퍼티를 전달하고자 할 때는 $ 접두사를 사용하자.
To pass a state property to another view in the view hierarchy, use the variable name with the $prefix operator. This retrieves a binding of the state property from its projectedValueproperty. For example, in the following code example PlayerView passes its state property isPlaying to PlayButton using $isPlaying:
struct PlayerView: View { var episode: Episode @State private var isPlaying: Bool = false var body: some View { VStack { Text(episode.title) Text(episode.showTitle) PlayButton(isPlaying: $isPlaying) } } }
Swift
복사