Learn Courses My Dashboard

Lesson 13 (14 Days Challenge)

I have a question about the bonus lesson 13 challenge. I got a decent amount of the way into completing this challenge before needing to refer to the solution for assistance. What I don’t understand is: Why do the first 3 @State vars all need to equal 1 in this situation? And in my HStack where I have the 3 assets show up: In the parenthesis, why does it have to be a string?

import SwiftUI

struct ContentView: View {
    
    @State var gameFruit = 1
    @State var middleMan = 1
    @State var mostRight = 1
    @State var creditAmount = 1000
    
    
    var body: some View {
        VStack {
        Spacer()
        
        Text("Swift UI Slots!")
            .font(.title)
            .padding(.all)
        
        Spacer()
        
            HStack {
        
                Text("Credits: \(creditAmount)")
            .font(.body)
        
                
            }
        Spacer()
        
        HStack {
            
            Spacer()
            Image("fruit\(gameFruit)")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .padding(.all)
            Spacer()
            Image("fruit\(middleMan)")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .padding(.all)
            Spacer()
            Image("fruit\(mostRight)")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .padding(.all)
                
            Spacer()
            
        }
        
        Spacer()
        
        Button("Spin"){
            
         
            gameFruit = Int.random(in: 1...3)
            middleMan = Int.random(in: 1...3)
            mostRight = Int.random(in: 1...3)
         
            if (gameFruit == middleMan && middleMan == mostRight) {
            creditAmount += 5
        } else {
            creditAmount -= 5
        }
        
        }
            
                .padding()
                .padding([.leading, .trailing], 30)
                .background(Color.red)
                .font(.title)
                .foregroundColor(Color.white)
                .cornerRadius(25)
                
            
                
            Spacer()
            
            
        }
        
        
        }
    
    }



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

@paulyd
Hi Paul,

They don’t necessarily have to be initialised to be 1 in each case. You could have them set to 1, 2 and 3 respectively. It really does not matter.

Later on when you tap on the Spin button the values are changed wth the code:

            gameFruit = Int.random(in: 1...3)
            middleMan = Int.random(in: 1...3)
            mostRight = Int.random(in: 1...3)

so they will change to reflect the random number selected.

I think I understand your question. Just in case I don’t are you referring to the code?:

Image("fruit\(gameFruit)")

If so, then Image requires a String parameter which is the name of the image hence that name being inside of two double quotes " ". Also when you want to combine a string and a variable then what that code does above is enable string interpolation with the interpolated value inside the \( and the )

Does that help?

Ok, but why does it need to be an Int?

Yes this makes total sense. Thank you.

It needs to be a value so that you can perform math operations on it such as generating a random value.