How can I unwrap an optional value inside a binding in Swift?

前端 未结 1 1800
悲&欢浪女
悲&欢浪女 2020-12-16 01:49

I\'m building an app using SwiftUI and would like a way to convert a Binding to a Binding>.

In my app I have an

相关标签:
1条回答
  • 2020-12-16 02:18

    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>?.

    0 讨论(0)
提交回复
热议问题