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

I have TabView in ContentView and I want to add TabView for OnboardingView in OtherView, every things work, but it is throw error for TabView in OtherView like "Trailing closure passed to parameter of type 'Int' that does not accept a closure" I do not know why? Any idea?

ContentView:

struct TabView : View {
   
  var body: some View{
     
    VStack(spacing: 0){
.......
}

OtherView:


   VStack {
    TabView {
      ForEach(onboardingData) { onboardingItem in
         OnboardingCard(onboardingItem: onboardingItem)
      }
  }
  .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
  .indexViewStyle(PageIndexViewStyle (backgroundDisplayMode:
  .always))
  .foregroundColor(.white)
}
Answered by skysoft13 in 684092022

As OOPers and Claude mentions in comment, when we called the TabView as MyTabView problem solved

Which line exactly ? Is it line 3 or 4 ? If so, please show how onboardingData and OnboardingCard are defined. Otherwise tell exactly where you get the error.

1.    VStack {
2.     TabView {
3.       ForEach(onboardingData) { onboardingItem in
4.          OnboardingCard(onboardingItem: onboardingItem)
5.       }
6.   }
7.   .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
8.   .indexViewStyle(PageIndexViewStyle (backgroundDisplayMode: .always))
9.   .foregroundColor(.white)
10. }

on line 2 I get error, in TabView , and  how onboardingData and OnboardingCard are defined it is defined as

OnboardingCard:


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

onboardingData:

  var onboardingData: [OnboardingItem] = [
  OnboardingItem(imageName: "image1", title: "Welcome1", description: "description1"),
  OnboardingItem(imageName: "image2", title: "Welcome2", description: "description2"),
    OnboardingItem(imageName: "image3", title: "Welcome3", description: "description3"),
  OnboardingItem(imageName: "image4", title: "Welcome4", description: "description4")
  ]

Doc states :

Tab views only support tab items of type Text, Image, or an image followed by text. Passing any other type of view results in a visible but empty tab item.

like in

TabView {
    Text("The First Tab")
        .badge(10)
        .tabItem {
            Image(systemName: "1.square.fill")
            Text("First")
        }

That's not the case with your code: you return OnboardingCard

ForEach(onboardingData) { onboardingItem in 
    OnboardingCard(onboardingItem: onboardingItem)
}

What is the purpose of VStack for TabView ?

Accepted Answer

As OOPers and Claude mentions in comment, when we called the TabView as MyTabView problem solved

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