user10991969
user10991969

Reputation:

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

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
}

Upvotes: 4

Views: 7819

Answers (2)

user10991969
user10991969

Reputation:

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

Upvotes: 2

mahan
mahan

Reputation: 14935

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)
}

Upvotes: 2

Related Questions