UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 21 - EXC_BAD_ACCESS

Forums > 100 Days of SwiftUI

Good day all!

I am receiving the follwoing error when running the guess the flag app. If I comment out the Text(countries[correctAnswer]), the issue goes away.

Xcode: 13.1 iOS 15 Builder

import SwiftUI

@main Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ff7b1375ff8) struct GuessTheFlagApp: App { var body: some Scene { WindowGroup { ContentView() } } }

Code in project:

//
//  ContentView.swift
//  GuessTheFlag
//
//  Created by Eric Forbes on 11/29/21.
//

import SwiftUI

struct ContentView: View {
    @State private var showingScore = false
    @State private var scoreTitle = ""

    @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)

    var body: some View {
        ZStack {
            Color.blue
            //            LinearGradient.init(colors: [.blue, .white], startPoint: .topLeading, endPoint: .bottomTrailing)
                .ignoresSafeArea()
            VStack(spacing: 30) {
                VStack {
                    Text("Tap the flag of")
                        .foregroundColor(.white)
                    Text(countries[correctAnswer])
                    foregroundColor(.white)
                }
                ForEach(0..<3) { number in
                    Button {
                        flagTapped(number)
                    } label: {
                        Image(countries[number])
                            .renderingMode(.original)
                    }
                }
            }
        }
        .alert(scoreTitle, isPresented: $showingScore) {
            Button("Continue", action: askQuestion)
        } message: {
            Text("Your score is ???")
        }
    }
    func flagTapped(_ number: Int) {
        if number == correctAnswer {
            scoreTitle = "Correct"
        } else {
            scoreTitle = "Wrong"
        }
        showingScore = true
    }
    func askQuestion() {
        countries.shuffle()
        correctAnswer = Int.random(in: 0...2)
    }
}

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

struct ContentView_Previews_Dark: PreviewProvider {
    static var previews: some View {
        ContentView().preferredColorScheme(.dark)
    }
}

2      

Text(countries[correctAnswer])
foregroundColor(.white)

should be:

Text(countries[correctAnswer])
    .foregroundColor(.white)

You forgot the leading . on the modifier.

4      

Thank you so much! The compiler didnt show any errors!

Just found that on Stackoverflow! Link here: https://stackoverflow.com/questions/69738422/xcode-swift-thread-1-exc-bad-access-code-2-address-0x7ffee99cdfd8

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.