Fix a crash in visionOS when nesting `.onChange` inside `#available` (#1984)
This commit is contained in:
Родитель
e4fa6fa4b5
Коммит
120798e3d9
|
@ -14,6 +14,13 @@ extension View {
|
|||
/// - action: A closure to run when the value changes.
|
||||
/// - Returns: A view that fires an action when the specified value changes.
|
||||
func onChange_iOS17<V>(of value: V, _ action: @escaping (V) -> Void) -> some View where V: Equatable {
|
||||
#if os(visionOS)
|
||||
// Known bug when using #available and self.onChange together in visionOS: it'll crash!
|
||||
// So for this OS, just use the new .onChange unconditionally.
|
||||
return self.onChange(of: value) { _, newValue in
|
||||
return action(newValue)
|
||||
}
|
||||
#else
|
||||
if #available(iOS 17, *) {
|
||||
return self.onChange(of: value) { _, newValue in
|
||||
return action(newValue)
|
||||
|
@ -21,5 +28,6 @@ extension View {
|
|||
} else {
|
||||
return self.onChange(of: value, perform: action)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,6 +67,13 @@ extension View {
|
|||
/// - action: A closure to run when the value changes.
|
||||
/// - Returns: A view that fires an action when the specified value changes.
|
||||
func onChange_iOS17<V>(of value: V, _ action: @escaping (V) -> Void) -> some View where V: Equatable {
|
||||
#if os(visionOS)
|
||||
// Known bug when using #available and self.onChange together in visionOS: it'll crash!
|
||||
// So for this OS, just use the new .onChange unconditionally.
|
||||
return self.onChange(of: value) { _, newValue in
|
||||
return action(newValue)
|
||||
}
|
||||
#else
|
||||
if #available(iOS 17, *) {
|
||||
return self.onChange(of: value) { _, newValue in
|
||||
return action(newValue)
|
||||
|
@ -74,6 +81,7 @@ extension View {
|
|||
} else {
|
||||
return self.onChange(of: value, perform: action)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче