Hi guys,
In the second challenge on day 24 I've written this code:
struct FlagImage: View {
var image: Image
var body: some View {
image
.renderingMode(.original)
.clipShape(Capsule())
.shadow(radius: 5)
}
}
Probably I didn't understand well what I have to do because it gives me an error when I use it in the Content View.
The error says: "Cannot convert value of type 'String' to expected argument type 'Image'"
struct ContentView: View {
@State private var showingScore = false
@State private var showingReset = false
@State private var scoreTitle = ""
@State private var userScore = 0
@State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled()
@State private var correctAnswer = Int.random(in: 0...2)
@State private var questionNumber = 1
var body: some View {
ZStack {
RadialGradient(stops: [
.init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
.init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3)
], center: .top, startRadius: 200, endRadius: 700)
.ignoresSafeArea()
VStack {
Spacer()
Text("Guess the Flag")
.font(.largeTitle.weight(.bold))
.foregroundColor(.white)
VStack(spacing: 15) {
VStack {
Text("Tap the flag of")
.foregroundStyle(.secondary)
.font(.subheadline.weight(.heavy))
Text(countries[correctAnswer])
.font(.largeTitle.weight(.semibold))
}
ForEach(0..<3) { number in
Button {
flagTapped(number)
} label: {
FlagImage(image: countries[number])
}
}
}
.frame(maxWidth: .infinity)
.padding(.vertical, 20)
.background(.regularMaterial)
.clipShape(RoundedRectangle(cornerRadius: 20))
Spacer()
Spacer()
Text("Score: \(userScore)")
.foregroundColor(.white)
.font(.title.bold())
Spacer()
}
.padding()
}
.alert(scoreTitle, isPresented: $showingScore) {
Button("Continue", action: askQuestion)
} message: {
Text("Your score is \(userScore)")
.foregroundColor(.white)
.font(.title.bold())
}
.alert(scoreTitle, isPresented: $showingReset) {
Button("Restart", action: reset)
} message: {
Text("Your final score is \(userScore)")
.foregroundColor(.white)
.font(.title.bold())
}
}
func flagTapped(_ number: Int) {
if number == correctAnswer {
scoreTitle = "Correct"
userScore += 1
} else {
scoreTitle = "Wrong That's the flag of \(countries[number])"
}
if questionNumber != 8 {
showingScore = true
} else {
showingScore = false
showingReset = true
scoreTitle = "Endgame"
}
}
func askQuestion() {
countries.shuffle()
correctAnswer = Int.random(in: 0...2)
questionNumber += 1
}
func reset() {
countries.shuffle()
correctAnswer = Int.random(in: 0...2)
userScore = 0
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I'll wait for your responses. Thanks.