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

WordScramble startGame() Fix

Forums > 100 Days of SwiftUI

Just finishing up the challenges for day 31. The second challenge asks you to add a bar button to allow you to start a new game. I added this but then realized that when it was pressed, the list of usedWords remained. To fix this, update the startGame method to the following:

func startGame() {
        if let startWordsURL = Bundle.main.url(forResource: "start", withExtension: "txt") {
            if let startWords = try? String(contentsOf: startWordsURL) {
                let allWords = startWords.components(separatedBy: "\n")
                rootWord = allWords.randomElement() ?? "silkworm"
                usedWords.removeAll() // this is the added line
                return
            }
        }

        fatalError("Could not load start.txt from bundle.")
    }

2      

If you just replace the .onAppear() with below code, it's done..navigationBarItems(trailing: Button(action: startGame) { Text("Restart Game") })

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

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.