我想问一下我使用时有关 SwiftUI 行为的问题.onChange( value) { }
为什么如果我使用@State var some: SomeType?可选类型,然后@Binding var some: SomeType此运算符仅检测到从某些 SomeType 值到 nil 的更改,反之亦然。但底层对象值的更改不会被检测为更改
前任。@Binging var 进度:Int?
将进度从 nil 更改为 100 会检测到更改,但如果我将值从 1 -> 2 -> 3 更改,它们就会被跳过 如果我使用@Binding var progress: Int
知道如何将选项与 onChange() 一起使用吗?
这是状态和绑定可选的工作示例:
\nimport SwiftUI\n\nstruct ContentView: View {\n    \n    @State private var progress: Int?\n    \n    var body: some View {\n        \n        CustomView(progress: $progress)\n        \n    }\n}\nstruct CustomView: View {\n    \n    @Binding var progress: Int?\n    \n    var body: some View {\n        \n        Button("update") {\n            \n            if let unwrappedInt = progress { progress = unwrappedInt + 1 } \n            else { progress = 0 }         //<< \xe2\x96\x88 \xe2\x96\x88 Here: initializing! \xe2\x96\x88 \xe2\x96\x88\n            \n        }\n        .onChange(of: progress) { newValue in\n            \n            if let unwrappedInt = progress { print(unwrappedInt) }\n            \n        }\n\n    }\n}\n| 归档时间: | 
 | 
| 查看次数: | 6684 次 | 
| 最近记录: |