[ad_1]
I’ve downside with fatalError(). I attempt to including to an inventory of phrases however on this line there may be downside it says ‘deadly Error in ContentView.swift’ and that i don’t why this occurred my code was completely right.
The code of this error;
// id had been are * right here* then there was an issue - set off a cradh and report the error
fatalError("couldn't load begin.txt from bundle.")
And the entire code is;
import SwiftUI
struct ContentView: View {
@State personal var usedWords = [String]()
@State personal var rootWord = ""
@State personal var newWord = ""
var physique: some View {
NavigationView {
Record {
Part {
TextField("enter your phrase", textual content: $newWord)
.textInputAutocapitalization(.by no means)
}
Part {
ForEach(usedWords, id: .self) { phrase in
HStack {
Picture(systemName: "(phrase.rely).circle")
Textual content(phrase)
}
}
}
}
.navigationTitle(rootWord)
.onSubmit(addNewWord)
.onAppear(carry out: startGame)
}
}
func addNewWord() {
// lowercase and trim the phrase, to verify we don:t add duplicate phrases with case variations
let reply = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
// exit if the remaining string is empty
guard reply.rely > 0 else { return }
// further validation to return
withAnimation {
usedWords.insert(reply, at: 0)
}
newWord = ""
}
func startGame() {
// 1.discover the URL for begin.txt in our app bundle
if let startWordsURL = Bundle.major.url(forResource: "begin",withExtension: "txt") {
// 2.load begin.txt right into a string
if let startWords = strive? String(contentsOf: startWordsURL) {
// 3.break up the string up into an array of strings, splitting on line breaks
let allWords = startWords.parts(separatedBy: "n")
// 4.pcik one random phrase, or use "yamori" as a wise default
rootWord = allWords.randomElement() ?? "yamori"
// if we're right here every little thing has labored, so. we are able to exit
return
}
}
// id had been are * right here* then there was an issue - set off a cradh and report the error
fatalError("couldn't load begin.txt from bundle.")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Any concept? Easy methods to repair that error
Thanks.
[ad_2]
