How can I unwrap an optional value inside a binding in Swift?
You can use this initialiser, which seems to handle this exact case - converting Binding<T?>
to Binding<T>?
:
var body: some View { AvatarView(userData: Binding($userById[activeUserId])!)}
I have used !
to force unwrap, just like in your attempts, but you could unwrap the nil
however you want. The expression Binding($userById[activeUserId])
is of type Binding<UserData>?
.