BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.