Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it says "Trailing closure passed to parameter of type 'Int' that does not accept a closure"?

Tags:

swift

swiftui

I added a second TabView in the SwiftUI project for Onboarding Screen, and it is throw an error like

Trailing closure passed to a parameter of type 'Int' that does not accept a closure

Any idea?

TabView {
    ForEach(0 ..< onboardingData.count) { index in
        let element = onboardingData[index]
        OnboardingCard(onboardingItem: element)
    }
}

OnboardingCard:

fileprivate struct OnboardingCard: View {
    let onboardingItem: OnboardingItem
    var body: some View {
        VStack {
            Image(onboardingItem.imageName)
                .resizable()
                .frame(height: 320)
                .frame (maxWidth: .infinity)
            Text(onboardingItem.title)
                .font(.title)
                .foregroundColor(.black)
                .bold()
                .padding()
            Text(onboardingItem.description)
                .multilineTextAlignment(.center)
                .font(.body)
                .foregroundColor(.gray)
                .padding (.horizontal, 15)
        }
    }
}
        
struct OnboardingItem {
    let imageName: String
    let title: String
    let description: String
}

2 Answers

I just run your code. It works as expected.


Confirm to the Identifiable protocol.

struct OnboardingItem: Identifiable {
    var id: UUID = UUID()
    let imageName: String
    let title: String
    let description: String
}

Doing so you avoid using and index based loop. And instead do this:

ForEach(onboardingData) { onboardingItem in
     OnboardingCard(onboardingItem: onboardingItem)
}
like image 65
mahan Avatar answered Sep 03 '25 01:09

mahan


when we rename the other TabView like MyTabView in the projects, problem solved, because project do not accept two TabView with the similar name.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!