'Whenever I add a spacer into my code. it crashes. It works fine until then

This is simple code I made to practice with spacers. This is SwiftUI.

    
    struct Spacer: View {
        var body: some View{
            HStack{
                Rectangle()
                    .frame(width: 100, height: 100)
                
                
                Rectangle()
                    .fill(Color.red)
                    .frame(width: 100, height: 100)
            }
            .background(Color.blue)
            
        }
    
    }
    
    struct Spacer_Previews: PreviewProvider {
        static var previews: some View {
            Spacer()
        }
    }
        


Solution 1:[1]

As @jnpdx already stated,

SwiftUI already has a type called Spacer. You should name your type something else.

Otherwise, you're confusing SwiftUI and it's not rendering a view in the previews - this triggers an error. It also most likely has an issue when you initialize it in the main app struct, because you're calling something other than a view there too.

If you do want just a spacer, then just use the normal spacer. Spacer(minLength: Int)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1