0

I am trying to assign value to property but getting bellow issue.

enter image description here

func buildClassViews(accessibilityID: String, data: Binding<[Passenger]>,  selectedPassengerId: inout String) -> [AnyView] {
    var views: [AnyView] = []
    @StateObject var viewModel: ViewModel
    
    for element in data {
        views.append(
            VStack {
                HStack {
                    VStack(alignment: .leading, spacing: 0) {
                        Text(element.fullName.wrappedValue ?? "")
                        Spacer()
                    }
                    .onAppear(perform: {
                        selectedPassengerId = "\(element.passengerId)"  //
                    })
                }
                .convertToAnyView())
            }
        }
}

Tried few things but not getting any clue....not sure what is this issue and how to resolve it.

New iOS Dev
  • 1,937
  • 7
  • 33
  • 67
  • Does this answer your question? [Swift 3.0 Error: Escaping closures can only capture inout parameters explicitly by value](https://stackoverflow.com/questions/39569114/swift-3-0-error-escaping-closures-can-only-capture-inout-parameters-explicitly) – Tushar Sharma Apr 23 '22 at 17:03
  • I doubt that you can *simulate* a SwiftUI `Binding` with a vanilla Swift `inout` parameter – vadian Apr 23 '22 at 17:04
  • is there any other way to do same? – New iOS Dev Apr 23 '22 at 17:18
  • 1
    It's unclear what you are going to accomplish. The @StateObject makes no sense either because a @StateObject is owned by a SwiftUI `View` and must be initialized. Apart form that it doesn't seem to be used anyway. – vadian Apr 23 '22 at 17:23
  • Your code also doesn't make sense as you will have one `onAppear` for each element in your data, so if it the code did compile, there will be multiple assignments made to `selectedPassengerId`. Can you explain what you are trying to do? – Paulw11 Apr 23 '22 at 20:44

0 Answers0