You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I'd like to execute an action that dismissed the SettingsView if it's state is non-nil from the userReducer, I could send an action like .binding(.set(\.settings?.isPresented, false)), but here Swift complains that it expects a WritableKeyPath, which is cased by SR-5820: Optional-chained KeyPath not Writable.
As Joe Groff points out in the above mentioned SR-5820, it's possible to compose key paths as follows
x?[keyPath: fooKeyPath]=1 // no-op if x == nil, otherwise assign x!.foo = 1
How would one go about introducing an extension to BindingAction that would allow to write to a potentially nil root value?
I have a hunch that writing an extension to BindingAction.set could solve it, but I can't get past making Root conform to Optional here... As well as BindingAction.init being private.
publicstaticfunc set<Value>(
_ keyPath:WritableKeyPath<Root,Value>,
_ value:Value)->Selfwhere Value:Equatable{.init(
keyPath: keyPath,
set:{$0?[keyPath: keyPath]= value },
value: value,
valueIsEqualTo:{ $0 as?Value== value })}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a UserState that contains optional SettingsState. In SettingsState
isPresented
property triggers SwiftUI view dismissal.If I'd like to execute an action that dismissed the SettingsView if it's state is non-nil from the
userReducer
, I could send an action like.binding(.set(\.settings?.isPresented, false))
, but here Swift complains that it expects a WritableKeyPath, which is cased by SR-5820: Optional-chained KeyPath not Writable.As Joe Groff points out in the above mentioned SR-5820, it's possible to compose key paths as follows
How would one go about introducing an extension to BindingAction that would allow to write to a potentially nil root value?
I have a hunch that writing an extension to BindingAction.set could solve it, but I can't get past making Root conform to Optional here... As well as
BindingAction.init
being private.Beta Was this translation helpful? Give feedback.
All reactions