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

SOLVED: Project 5: WordScramble won't find file

Forums > 100 Days of SwiftUI

I have already completed this project over a year ago with no problem. But I kind of fell off of my Swift education since then, and have decided to try to complete the 100 days again to refresh my memory.

I am currenty working on project 5, and can't figure out how to make this function work

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"
                return
            }
        }
        fatalError("Could not load start.txt from bundle.")
        }
    }

Has something changed with the way that Xcode adds assets to a project? Or with the way that we are supposed to use Bundle to retrieve them?

When I look at my old project from when I completed it before, the "start.txt" file is located directly inside of the "WordScramble" project folder. But in my new attempt at the project the "start.txt" file is located in "WordScramble/Assets.xcassets/start.dataset/start.txt"

Both times I just dragged the start.txt file into the assets area of my project in xcode as far as I can remember. So it seems like something has changed.

1      

@Bnerd  

I have the start.txt file directly under the ConteView.swift file I used your function on my app and it works. https://imgur.com/Xf4tRuV

2      

I realized that I should have just been dragging the file into the project navigator and not into the assets section. That is what was causing my problem.

1      

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.