Search code examples
swiftuienvironmentobjectswiftui-foreach

Using ForEach(data:,content:) with an @EnvironmentObject in SwiftUI without resorting to trailing closure syntax?


I am refactoring my code, and want to use a memberwise initializer with ForEach(data:,content:).

I have a viewModel with an array of structs that conform to Identifiable:

class ViewModel: ObservableObject {
    @Published var int = 0
    
    var cellModels: [CellModel]
    
    init() {
        self.cellModels = [CellModel(id: 0, string: "Foo"),
                           CellModel(id: 1, string: "Bar")]
    }
}

struct CellModel: Identifiable {
    var id: Int
    var string: String
}

then I have created a CellView that has a memberwise initialiser from a CellModel and also has an EnvironmentalObject which is an instance of the ViewModel:

struct CellView: View {
    @EnvironmentObject var environment: ViewModel
    var cellModel: CellModel
    var body: some View {
        Text(cellModel.string)
    }
}

However when I create the parent view I get the compiler error on the ForEach(data:, content:) line:

struct DemoView: View {
    @StateObject var viewModel = ViewModel()
    var body: some View {
        VStack {
            ForEach(viewModel.cellModels, content: CellView.init) // Compiler error is here
// Cannot convert value of type '(EnvironmentObject<ViewModel>, CellModel) -> CellView' to expected argument type '(CellModel) -> CellView'
        }
        .environmentObject(viewModel)
    }
}

I understand that this is because ForEach(data:,content:) is trying to pass both the EnvironmentObject and the individual model to each CellView, but is there a way to do this without having to use ForEach(data:,content:) with a trailing closure? :

ForEach(viewModel.cellModels){ cellModel in
                CellView(cellModel: cellModel)
            }

Solution

  • Use init with without argumentLabel

    struct CellView: View {
        @EnvironmentObject var environment: ViewModel
    
        private var cellModel: CellModel
    
        init(_ cellModel: CellModel) {
            self.cellModel = cellModel
        }
    
        var body: some View {
            Text(cellModel.string)
        }
    }
    
    struct DemoView: View {
        @StateObject var viewModel = ViewModel()
        var body: some View {
            VStack {
                ForEach(viewModel.cellModels, content: CellView.init)
            }
            .environmentObject(viewModel)
        }
    }