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

import SwiftUI

struct ContentView: View {
private var symbols = ["food1", "food2", "food3"]
@State private var numbers = [1, 2, 0, 3]
@State private var credits = 1000
private var betAmount = 5

        var body: some View {
            ZStack {
                var debug: some View {
                    MyViewWithError(property: self.property)
                }
           
                       //Background:
                       Circle()
                           .foregroundColor(Color (red: 90/255, green: 200/255, blue: 250/255))
                           .edgesIgnoringSafeArea(.all)
                       
                       Circle()
                           .foregroundColor(Color (red: 250/255, green: 45/255, blue: 85/255))
                           .rotationEffect(Angle(degrees: 45))
                           .edgesIgnoringSafeArea(.all)
                        .shadow(radius: 10)
                        .overlay(Circle().stroke(Color.yellow, lineWidth: 5))

                
                VStack {
                    Spacer()
                    
                    //Title:
                    HStack {
                        Image(systemName: "sun.min")
                            .foregroundColor(.black)
                        
                        Text("Eatable Turbine")
                            .bold()
                            .foregroundColor(.white)
                        
                        Image(systemName: "sun.min")
                        .foregroundColor(.black)
                    }.scaleEffect(2)
                    
                   
                    
                    Spacer()
                    //cards
                    HStack {
                        
                        Spacer()
                        
                        Image(symbols[numbers[0]])
                            .resizable()
                            .aspectRatio(1, contentMode: .fit)
                            .background(Color.white.opacity(0.5))
                            .cornerRadius(10)

                        Image(symbols[numbers[1]])
                            .resizable()
                            .aspectRatio(1, contentMode: .fit)
                            .background(Color.white.opacity(0.5))
                            .cornerRadius(10)
                        
                        Image(symbols[numbers[2]])
                            .resizable()
                            .aspectRatio(1, contentMode: .fit)
                            .background(Color.white.opacity(0.5))
                            .cornerRadius(10)
                        
                        Image(symbols[numbers[3]])
                                                       .resizable()
                            .aspectRatio(0.5, contentMode: .fit)
                                                       .background(Color.white.opacity(0.5))
                                                       .cornerRadius(20)
                        
                     
                        Image()
                            .frame(width: 32.0, height: 32.0)
                        
                    
                        Spacer()
                        
                    }
                    
                    Spacer()
                //Button
                    Button(action: {
                        
                        //Change the images
                        
                        
                        self.numbers[0] = Int.random(in:
                            0...self.symbols.count - 1)
                        
                        self.numbers[1] = Int.random(in:
                            0...self.symbols.count - 1)

                        
                        self.numbers[2] = Int.random(in:
                            0...self.symbols.count - 1)
                        
                        //Check winnings
                        if self.numbers[0]
                            == self.numbers[1] && self.numbers[1] == self.numbers[2]{
                        
                        }
                        
                    })  {
                        Text("Spin")
                        .bold()
                            .foregroundColor(.white)
                            .padding(.all, 10)
                            .padding([.leading, .trailing], 30)
                            .background(Color.blue)
                            .cornerRadius(20)
                        
            }
            Spacer()
                    
                    
                    
        }
    }
}

}

I really need help, because my xcode project isn't working because of this bug! Thanks!!! (Also, the bug is towards the front, where it says ZStack and the var debug too! Thanks!

This isn't really appropriate for this forum. It's about SwiftUI, which is an Apple private framework.

However, I've reproduced the essence of your code here, and it exhibits the error. Issue is that you are not allowed to include a closure (your var debug computed variable) within a function builder.

import SwiftUI

struct ContentView: View {

	var body: some View {
		ZStack {
			var debug: some View {
				MyViewWithError(property: self.property)
			}
		}
	}
}

It you take the debug computed variable (the closure) out, it should be ok.

Hi, I'm sorry to include that in this forum, but let me send you the whole code. When I deleted the var, it just showed the bug again. Thanks! Here it is:

import SwiftUI

struct ContentView: View {
private var symbols = ["food1", "food2", "food3"]
@State private var numbers = [1, 2, 0, 3]
@State private var credits = 1000
private var betAmount = 5

        var body: some View {
            ZStack {
                var debug: some View {
                    MyViewWithError(property: self.property)
                }
           
                       //Background:
                       Circle()
                           .foregroundColor(Color (red: 90/255, green: 200/255, blue: 250/255))
                           .edgesIgnoringSafeArea(.all)
                       
                       Circle()
                           .foregroundColor(Color (red: 250/255, green: 45/255, blue: 85/255))
                           .rotationEffect(Angle(degrees: 45))
                           .edgesIgnoringSafeArea(.all)
                        .shadow(radius: 10)
                        .overlay(Circle().stroke(Color.yellow, lineWidth: 5))

                
                VStack {
                    Spacer()
                    
                    //Title:
                    HStack {
                        Image(systemName: "sun.min")
                            .foregroundColor(.black)
                        
                        Text("Eatable Turbine")
                            .bold()
                            .foregroundColor(.white)
                        
                        Image(systemName: "sun.min")
                        .foregroundColor(.black)
                    }.scaleEffect(2)
                    
                   
                    
                    Spacer()
                    //cards
                    HStack {
                        
                        Spacer()
                        
                        Image(symbols[numbers[0]])
                            .resizable()
                            .aspectRatio(1, contentMode: .fit)
                            .background(Color.white.opacity(0.5))
                            .cornerRadius(10)

                        Image(symbols[numbers[1]])
                            .resizable()
                            .aspectRatio(1, contentMode: .fit)
                            .background(Color.white.opacity(0.5))
                            .cornerRadius(10)
                        
                        Image(symbols[numbers[2]])
                            .resizable()
                            .aspectRatio(1, contentMode: .fit)
                            .background(Color.white.opacity(0.5))
                            .cornerRadius(10)
                        
                        Image(symbols[numbers[3]])
                                                       .resizable()
                            .aspectRatio(0.5, contentMode: .fit)
                                                       .background(Color.white.opacity(0.5))
                                                       .cornerRadius(20)
                        
                     
                        Image()
                            .frame(width: 32.0, height: 32.0)
                        
                    
                        Spacer()
                        
                    }
                    
                    Spacer()
                //Button
                    Button(action: {
                        
                        //Change the images
                        
                        
                        self.numbers[0] = Int.random(in:
                            0...self.symbols.count - 1)
                        
                        self.numbers[1] = Int.random(in:
                            0...self.symbols.count - 1)

                        
                        self.numbers[2] = Int.random(in:
                            0...self.symbols.count - 1)
                        
                        //Check winnings
                        if self.numbers[0]
                            == self.numbers[1] && self.numbers[1] == self.numbers[2]{
                        
                        }
                        
                    })  {
                        Text("Spin")
                        .bold()
                            .foregroundColor(.white)
                            .padding(.all, 10)
                            .padding([.leading, .trailing], 30)
                            .background(Color.blue)
                            .cornerRadius(20)
                        
            }
            Spacer()
                    
                    
                    
        }
    }
}

}

In a swift playground, taking the debug var out, you are left with an error in one of your Image views that is null. What error are you getting?

After I take the debug var out, I'm left with this: Unable to infer complex closure return type; add explicit type to disambiguate

Thanks!

What line in your code is showing the error? Once I realized you were using iOS, I moved your code to an iOS playground and eliminated the Image() view declaration. Everything compiled fine.

OMG! You are a life-saver!!! It worked!!!

Terms of Service

Privacy Policy

Cookie Policy