โดยพื้นฐานแล้วฉันการซ้อนใน @Binding
3 ชั้นลึกมาก
struct LayerOne: View {
@State private var doubleValue = 0.0
var body: some View {
LayerTwo(doubleValue: $doubleValue)
}
}
struct LayerTwo: View {
@Binding var doubleValue: Double {
didSet {
print(doubleValue)
}
}
var body: some View {
LayerThree(doubleValue: $doubleValue)
}
}
struct LayerThree: View {
@Binding var doubleValue: Double {
didSet {
print(doubleValue) // Only this print gets run when doubleValue is updated from this struct
}
}
var body: Some view {
// Button here changes doubleValue
}
}
แล้วแต่ตาม struct ฉันเปลี่ยน doubleValue
ในคืนที่หนึ่งอยู่ที่ไหน didSet
จะได้วิ่งหนีดังนั้นสำหรับตัวอย่างเช่นถ้าฉันเปลี่ยนแปลงมันอยู่ LayerThree
แค่นั้นใครจะพิมพ์เรื่องของคนอื่นจะทำเอง
ฉันสามารถเฝ้าดูการเปลี่ยนแปลงของกัน .onChange(of: doubleValue)
ซึ่งจะให้วิ่งหนีตอนที่มันจะเปลี่ยนแต่มันไม่ใช่การตอบสนกับฉันทำไม didSet
ไม่ได้วิ่งยกเว้นที่ struct ยู่ที่ไหนมันเปลี่ยนแปลงจาก.
นี่ @Binding
struct เศรษฐกิจของสหรัฐ