๐ฅState
Last updated
Last updated
SwiftUI โฉ Data Flow โฉ State
ๆณจๆ๏ผ@State ่ @Binding ๅช็จๆผ value typeโ
State ่ฎๆธๅฏไปฅ็จ initializer ๅๅงๅ ๐ ็ฏไพ๏ผ Swatch
ๅ๏ผCan we change a State var in ViewBuilderโ
็ญ๏ผ
... SwiftUI is a recipe, it's not an event system. You don't change state variables in the body
function, you set up how things should react when state variables change externally. ๐ StackOverflow
State is a value, or a set of values, that can change over time, and that affects a viewโs behavior, content, or layout.
In SwiftUI, a property thatโs marked with @State (or @Binding), isnโt just one property. Itโs three properties! ๐ The Inner Workings of State Properties of SwiftUI - Medium
ๅฐๆฏไธๅ @State var state: T
่ฎๆธไพ่ชช๏ผๅฎ็ๅ
ง้จ้ฝ้ฑๅซไบใไธๅ่ฎๆธใ๏ผ
_state: State<T> ๅฐฑๆฏ @State ่ฎๆธๆฌ่บซ๏ผๅๅงๅๆ็จๅฐ็ๅๅงๅผ (initial value) ๆๆพๅจ _state.wrappedValue ่ฃก้ขใ
$state: Binding<T> ๅฎๆๅๅณ _state.projectedValue๏ผ็จไพๅใ้ๅ้ฃ็ตใ็จ็ใ
state: T ้ๆฏไธๅ computed property๏ผ็จไพๅๅณๆ่จญๅฎ _state.wrappedValueใ
โจ Swatch - initialze State var, report view preference.
struct LandmarkList: View {
/// โญ๏ธ state is always private
@State private var showFavoritesOnly = true
var body: some View { ... }
}
SwiftUI Tutorials โฉ Handling User Input โฉ Sec. 2: Filter the List View
SwiftUI โฉ State and Data Flow
State (struct)
SwiftOnTap โฉ State
Swatch shows how to initialize a State variable in an initializer.
SwiftUI @State var initialization issue - how to init a State var?
A @State static property is being reinitiated without notice
โญ๏ธ don't use a @State static private var
โ