When I try using an @State property wrapper in my first struct, FlagImage, it doesn't work. Xcode gives me this error: "'FlagImage' initializer is inaccessible due to 'private' protection level" - Why is it doing this? I don't really understand the error.
I took @State off and it works just fine. Below is my code of the app that works without the property wrapper
import SwiftUI
struct FlagImage: View {
var flag: String
var body: some View {
Image(flag)
.renderingMode(.original)
.clipShape(Capsule())
.shadow(radius: 8)
}
}
struct Title: ViewModifier {
func body(content: Content) -> some View {
content
.font(.largeTitle)
.foregroundColor(.accentColor)
.padding()
}
}
extension View {
func titleStyle() -> some View {
modifier(Title())
}
}
struct ContentView: View {
@State private var showingScore = false
@State private var scoreTitle = ""
@State private var userScore = 0
@State private var maxNumberOfQuestions = 8
@State private var questionTitle = ""
@State private var endGame = false
@State private var currentQuestion = 1
@State private var correctAnswer = Int.random(in: 0...2)
@State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled()
var body: some View {
ZStack {
RadialGradient(gradient: Gradient(colors: [Color("Light Pink"), Color("Light Blue")]), center: .bottom, startRadius: 200, endRadius: 400)
.ignoresSafeArea()
VStack {
Spacer()
HStack {
Image(systemName: "flag.filled.and.flag.crossed").foregroundColor(.black)
Text("Guess the Flag")
.titleStyle()
Image(systemName: "flag.and.flag.filled.crossed").foregroundColor(.black)
}
VStack(spacing: 15) {
Text("Tap the flag of")
.foregroundStyle(.secondary)
.font(.subheadline.weight(.heavy))
Text(countries [correctAnswer]).font(.largeTitle.weight(.semibold))
ForEach(0..<3) { number in
Button {
print("tapped \(countries[number])")
flagTapped(number)
} label: {
FlagImage(flag: countries[number])
}
}
}
.frame(maxWidth:350)
.padding(.vertical, 20)
.background(.thinMaterial)
.clipShape(RoundedRectangle(cornerRadius: 100))
Spacer()
Spacer()
Text("Score: \(userScore)")
.foregroundColor(.black)
.font(.title.bold())
Text("Question: \(currentQuestion)")
.foregroundColor(.black)
.font(.title.bold())
Spacer()
}
.padding()
}
.alert(questionTitle, isPresented: $endGame) {
Button("RESTART GAME", action: newGame)
} message: {
Text("Congrats! Final Score: \(userScore).")
}
.alert(scoreTitle, isPresented: $showingScore) {
Button("Continue", action: askQuestion)
} message: {
Text("Your Score: \(userScore).")
}
}
func flagTapped(_ number: Int) {
if number == correctAnswer {
scoreTitle = "Correct"
userScore += 10
} else {
scoreTitle = "Wrong! It's the flag of \(countries[correctAnswer])."
}
showingScore.toggle()
}
func askQuestion() {
if currentQuestion < 8 {
countries.shuffle()
currentQuestion += 1
correctAnswer = Int.random(in: 0...2)
} else {
endGame.toggle()
}
}
func newGame() {
if currentQuestion == 8 {
userScore = 0
currentQuestion = 0
countries.shuffle()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}