UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Failed to produce diagnostic for expression; please submit a bug report

Forums > SwiftUI

I'm having an issue that I can't seem to figure out what might be the problem. I have a custom view that works fine outside of a ForEach, however the same custom view generates this error for the top level view once it is in a ForEach.

Error: Failed to produce diagnostic for expression; please submit a bug report Location:

var body: some View { // <-- Location of error

Example of it working outside of ForEach

CategoryPill(category: .breakfast, tracked: $vm.tracked.food) {
    print("Tapped category pill")
}

This generates the error:

ForEach(allCategories) { category in
    CategoryPill(category: category, tracked: $vm.tracked.food) {

    }
}

However, the same ForEach works fine with anything else in it...

ForEach(allCategories) { category in
    Text("asdas") // <-- This Works, no error
}

I even tried this thinking it was the sytax used for the action:

ForEach(allCategories) { category in
    CategoryPill(category: category, tracked: $vm.tracked.food, action: {}) // <-- Still generates the error
}

Any insight is much appreciated. If I need to provide additional context let me know.

Thanks!

1      

hi mythical,

sorry, this is definitely one that requires more code for context.

the SwiftUI compiler is not wholly reliable in pinpointing errors within the body definition, depending on the complexity of the view. it can often default to citing the body line as the error; and the error could be somewhere other than where you think it is.

suggestion: code for the top level view and the CategoryPill view (trimmed down if possible, as long as the error still remains). it would also be helpful to know a little more about the @State var vm.

you may get more of a response then,

DMG

1      

No worries, I had a feeling it would need more code. Here is a trimmed down body for the view, the body for the categorypill and the vm class. I trimmed it down some and the error does still occur. The app still uses RxSwift. I haven't converted it to Combine yet in case that might matter....

Thanks!

@StateObject var vm: DashboardViewModel = DashboardViewModel()

var body: some View {
    NavigationView {
        ScrollView {
            VStack(spacing: 8) {

                VStack(spacing: 10) {

                    ForEach(allCategories) { category in
                        // This line seems to cause the error... When it's commented out the error goes away.
                        CategoryPill(category: category, tracked: $vm.tracked.food, action: {}) 
                    }

                    CategoryPill(category: .breakfast, tracked: $vm.tracked.food) {
                        print("Tapped category pill")
                    }
                    Spacer()
                }
                .padding(.horizontal, 24)
                Spacer()
            }
        }
        .navigationBarHidden(true)
    }
}

CategoryPill body

var category: LocalFoodCategory
    @Binding var tracked: LocalTrackedFoodResults?
    let action: (() -> Void)

    var body: some View {
        ZStack {

            RoundedRectangle(cornerRadius: 8)
                .fill(.white)
                .shadow(color: Color(uiColor: .darkGray), radius: 2, x: 2, y: 2)
                .frame(maxWidth: .infinity, maxHeight: .infinity)

            HStack(spacing: 10) {
                category.iconAsImage
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 40)

                VStack(alignment: .leading) {
                    Text(category.displayTitle)
                        .foregroundColor(C2GColor.titleColor.asColor())
                        .font(C2GFont.semiBold.asFont(ofSize: 18))
                        .layoutPriority(1)
                        .frame(maxWidth: .infinity, alignment: .leading)
                    Text(_getTrackedText())
                        .foregroundColor(C2GColor.titleColor.asColor())
                        .font(C2GFont.medium.asFont(ofSize: 10))
                        .frame(maxWidth: .infinity, alignment: .leading)

                }
                .padding(.vertical, 8)
                .frame(maxWidth: .infinity)

                _getActionImage()
                    .renderingMode(.template)
                    .foregroundColor(C2GColor.buttonMain.asColor())
                    .frame(width: 20)
            }
            .padding(8)
            .frame(maxWidth: .infinity, maxHeight: .infinity)

        }
        .padding(.horizontal, 8)
        .frame(height: 74)
        .onTapGesture {
            action()
        }
    }

Contents of vm

class DashboardViewModel: ObservableObject {

    let foodLogRepository = FoodLogDataRepository(local: FoodLogCoreDataDatasource())
    let disposeBag = DisposeBag()

    @Published var tracked: LocalTrackedFoodResults?

    func getHistroy(date: Date) {

        let useCase = GetFoodLogForDateUseCase(foodLogRepository)
        useCase.execute(requestValues: GetFoodLogForDateUseCaseRequestValues(date: date))
            .subscribe(onNext: { [weak self] (results) in
                self?.tracked = results
            }, onError: { [weak self] (error) in
                self?.tracked = nil
            }).disposed(by: disposeBag)

    }

}

1      

Nevermind... I figured it out. It was in the $vm... the type the CategoryPill was expecting was not what I was passing.

Lack of sleep. I apologize :)

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.