Learn Courses My Dashboard

Why is this error popping up?

Hey everyone, I get this error that says:

Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

My code:

import SwiftUI

struct HomeCarousel: View {
    var body: some View {
        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
    }
}

struct HomeCarousel_Previews: PreviewProvider {
    static var previews: some View {
        HomeCarousel()
    }
}

struct Home : View {
    @State var index = 0
    @State var stories = [
        Story(id: 0, image: "p0", offset: 0,title: "Jack the Persian and the Black Castel"), Story(id: 1, image: "p1", offset: 0,title: "The Dreaming Moon"),
        Story(id: 2, image: "p2", offset: 0,title: "Fallen In Love"),
        Story(id: 3, image: "p3", offset: 0,title: "Hounted Ground"),
    ]
    @State var scrolled = 0
    @State var index1 = 0
    
    var body: some View{
        ScrollView(.vertical, showsIndicators: false) {// error
            VStack{
                HStack{
                    Button(action: {}) {
                        Image("menu") .renderingMode(.template) .foregroundColor(.white)
                        
                    }
                    
                    Spacer()
                    
                    Button(action: {}) {
                        Image("search") .renderingMode(.template) .foregroundColor(.white)
                        
                    }
                }
                .padding()
                
                HStack{
                    Text("Trending")
                        .font(.system(size: 40, weight: .bold)) .foregroundColor(.white)
                    
                    Spacer(minLength: 0)
                    
                    Button(action: {}) {
                        Image("dots")
                            .renderingMode(.template) .foregroundColor(.white) .rotationEffect(.init(degrees: 90))
                        
                    }
                    
                }
                .padding(.horizontal)
                
                HStack{
                    Text("Animated")
                        .font(.system(size: 15)) .foregroundColor(index == 0 ? .white : Color("Color1")
                        .opacity(0.85))
                        .fontWeight(.bold)
                        .padding(.vertical,6)
                        .padding(.horizontal,20)
                        .background(Color("Color")
                        .opacity(index == 0 ? 1 : 0))
                        .clipShape(Capsule())
                        .onTapGesture {
                            index = 0
                            
                        }
                                                                
                                                                
                    
        Text("25+ Stories")
            .font(.system(size: 15))
            .foregroundColor(index == 1 ? .white : Color("Color1").opacity(0.85)) .fontWeight(.bold)
            .padding(.vertical,6)
            .padding(.horizontal,20)
            .background(Color("Color")
            .opacity(index == 01 ? 1 : 0))
            .clipShape(Capsule())
            .onTapGesture {
                index = 1
            }
            Spacer()
                }
                .padding(.horizontal)
                .padding(.top,10)
                ZStack{
            ForEach(stories.reversed()){ story in
                HStack{
            ZStack(alignment: Alignment(horizontal: .leading, vertical: .bottom)){
                Image(story.image)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: calculateWidth(), height: (UIScreen.main.bounds.height / 1.8) - CGFloat(story.id - scrolled) * 50) .cornerRadius(15)
                
                VStack(alignment: .leading,spacing: 18){ HStack{
                    Text(story.title)
                        .font(.title)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                    Spacer()
                    
                }
                
                
                Button(action: {}) {
                    Text("Read Later")
                        .font(.caption)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                        .padding(.vertical,6)
                        .padding(.horizontal,25)
                        .background(Color("Color1"))
                        .clipShape(Capsule())
                    
                }
                
                }
                .frame(width: calculateWidth() - 40)
                .padding(.leading,20)
                .padding(.bottom,20)
                
            }
            .offset(x: story.id - scrolled <= 2 ? CGFloat(story.id - scrolled) * 30 : 60)
                    
                Spacer(minLength: 0)
                    
                } .contentShape(Rectangle())
                .offset(x: story.offset) .gesture(
                    DragGesture().onChanged({ (value) in withAnimation{
                        if value.translation.width < 0 &&
                            story.id != stories.last!.id{
        stories[story.id].offset = value.translation.width
                       
} else{
    if story.id > 0{
        stories[story.id - 1]
            .offset = -(calculateWidth() + 60) + value.translation.width
        
    }
    
}
}
    
})
.onEnded({ (value) in
    
    
    withAnimation{
        if value.translation.width < 0{
            if -value.translation.width > 180 && story.id != stories.last!.id{
                stories[story.id].offset = -(calculateWidth() + 60)
                scrolled += 1
                
            } else{
                
                stories[story.id].offset = 0
                
            }
            
        } else{
            if story.id > 0{
                if value.translation.width > 180{
                    stories[story.id - 1]
                        .offset = 0
                    scrolled -= 1
                    
                } else{
                stories[story.id - 1]
                    .offset = -(calculateWidth() + 60)
                    
                }
                
            }
            
        }
        
    }
    
}))
}
                

                
.frame(height: UIScreen.main.bounds.height / 1.8)
.padding(.horizontal,25)
.padding(.top,25)
}
func calculateWidth()->CGFloat {
    let screen = UIScreen.main.bounds.width - 50
    let width = screen - (2 * 30)
    return width
    
}

        }
    }
}
}

struct Story : Identifiable {
    var id : Int
    var image : String
    var offset : CGFloat
    var title : String
}
 

You have the calculateWidth function contained within the body of your View. Can’t do that.

Your code is rather hard to read, to be honest. Try selecting all your code and using ⌃I to reindent everything and these kinds of problems will become clearer.

1 Like