0

I want to pass a binding variable inside foreach.

struct Users: View {
    let users: [User]
    
    var body: some View {
        ForEach(users){ user in
            NavigationLink(destination: UserEdit(user: user), label: {
                UserMemberCell(user: user)
            })
        }
    }
}

struct UserEdit: View {
    @Binding var user:User

    init(user:Binding<User>){
        self._user = user
    }
    var body: some View {
        Text(user.name)
    }
}

Because the user property's change should pass in any related views. users array is just let array. and user is also not publisher.

How to wrap user into user's binding variable in ForEach.

  • 1
    Duplicate. [@Binding and ForEach in SwiftUI](https://stackoverflow.com/questions/57340575/binding-and-foreach-in-swiftui). Also, do not post partial code that does not compile. https://stackoverflow.com/help/minimal-reproducible-example –  Mar 19 '23 at 14:41
  • I use just `let` array. The link you gave uses `publisher`. How do I resolve my code? – taro Tanaka Mar 19 '23 at 14:54
  • That question seems like nonsense to me. Where do you see "`publisher`"? –  Mar 19 '23 at 15:02
  • You can't have a `let` you need to reference your source of truth with `Binding` watch Demystify SwiftUI you haven't provided enough information to give you a specific answer – lorem ipsum Mar 19 '23 at 15:04

0 Answers0