this post was submitted on 02 Jul 2023
2 points (100.0% liked)

Hacking with Swift

93 readers
1 users here now

This community is not affiliated nor endorsed by Hacking with Swift or Paul Hudson

This is a community dedicated to people who use Paul Hudson's Hacking with Swift education materials to learn app development and improve their skills.

Feel free to share your progress with 100 Days of Swift(UI), ask questions about projects you're struggling with or seek assistance in your learning.

We expect you to be civil and constructive as we aim to try and mirror the respectful and helpful climate of other HWS communities across the internet.

Piracy:


Other communities on related topics

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

founded 1 year ago
MODERATORS
 

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

top 2 comments
sorted by: hot top controversial new old
[โ€“] HeavyDogFeet 1 points 11 months ago* (last edited 11 months ago) (1 children)

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 1 points 11 months ago

Fixed it. I realised I had the modifiers on the NavigationView, not the List. Such a simple thing to overlook ๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ