You can use this thread to ask questions if you need help with something or need help understanding something

  • HeavyDogFeet@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    1 year ago

    Can someone help me figure out what’s going on here?

    I’ve working on the Word Scramble project (project 5 I believe), following Paul along as he lays out the basics of the app.

    I’ve reached the end of “Running code when our app launches” module, and the video ends with a random word being picked from the txt list and shown at the top of the app. Paul shows it working in his code, but for whatever reason it’s not showing up in mine. I’ve checked his code and mine side by side and it’s identical other than some interface text strings.

    Any idea what’s going on? Am I missing something super obvious?

    Here’s my code:

    import SwiftUI
    
    struct ContentView: View {
        @State private var usedWords = [String]()
        @State private var rootWord = ""
        @State private var newWord = ""
        
        var body: some View {
            NavigationView {
                List {
                    Section {
                        TextField("Enter your word", text: $newWord)
                            .autocapitalization(.none)
                    }
                    
                    Section {
                        ForEach(usedWords, id: \.self) { word in
                            HStack {
                                Image(systemName: "\(word.count).circle")
                                Text(word)
                            }
                        }
                    }
                }
            }
            .navigationTitle(rootWord)
            .onSubmit(addNewWord)
            .onAppear(perform: startGame)
        }
        
        func addNewWord() {
            let answer = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
            guard answer.count > 0 else { return }
            
            withAnimation {
                usedWords.insert(answer, at: 0)
            }
            
            newWord = ""
        }
        
        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.")
        }
        
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    • HeavyDogFeet@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Fixed it. I realised I had the modifiers on the NavigationView, not the List. Such a simple thing to overlook 🤦🏼‍♂️