Home > other >  when i try to make a label it gives me an error - Cannot convert value of type 'String' to
when i try to make a label it gives me an error - Cannot convert value of type 'String' to

Time:08-16

ive been trying to make a game that is basically a cookie clicker rip off..but with pies. i had such high hopes with barely any prior knowledge on this and what do you know i got stuck right away <_< shocker ,, i know right !! all jokes aside ive tried adding : string inside of the \(pies) (like this Label("You have \(pies ; String) pies.")) but that gave me the same error :( idk what to do so if anyone could help me minimize this error that would be greatly appreciated !! also the label is supposed to display how many pies the user has ( 1 for every click on the button) and ive gotten my pie counting system to work (i think) but i just cant display it. hopes this adds a bit more context to this xd xd also i tried to use text but that also gave me an error :] thanks in advance !!!!

heres my ContentView.swift file -


func pieClicked(){
    pies = pies   1
}

struct ContentView: View {
    

    var body: some View {
        
        VStack {
            
        Label("You have \(pies) pies.")
            
        
        Button("Button") {
            pieClicked()
            }
            
        Image("Pie")
            .aspectRatio(contentMode: .fit)
            
        Text("Click the pie.")
            .padding()
        
        }
        
        
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

CodePudding user response:

I have no idea what kind of UI you're trying to achieve. If you need to display the new pies count each time the button is clicked, use the code below.

struct ContentView: View {
    
    @State var pies = 0
    
    var body: some View {
            
        VStack(spacing: 20) {
            Text("You have \(pies) pies.")
            Button("Button") {
                pieClicked()
            }
            Image("Pie")
                .aspectRatio(contentMode: .fit)
            Text("Click the pie.")
                .padding()
        }
    }
    
    func pieClicked() {
        pies = pies   1
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

CodePudding user response:

Maybe Something like this:

struct ContentView: View {
    @State var pies = 0

    var body: some View {
        VStack(spacing: 30) {

            Text("You have \(pies) pies")
                .bold()
                .shadow(color: .primary, radius: 1, x: 1, y: 1)
        
            Button {
                pieClicked()
            } label: {
                Label {
                    Text("Click the pie")
                        .bold()
                } icon: {
                    Image(systemName: "chart.pie.fill")
                        .imageScale(.large)
                        .shadow(color: .primary, radius: 2, x: 2, y: 2)
                }
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.yellow.opacity(0.4))
        .foregroundColor(.orange)
        .font(.title)
    }

    func pieClicked() { pies = pies   1 }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
  • Related