GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Possible typo on Adding to a list of words

Forums > 100 Days of SwiftUI

On the Word Scramble page, where we are adding the addNewWord() method, it says:

"...we can do that by adding an onSubmit() modifier somewhere in our view hierarchy โ€“ it could be directly on the button, but it can be anywhere else in the view..."

... but that isn't a button! Perhaps Paul meant to say "it could be directly on the TextField"?

... or is there a button somewhere that I'm missing?

... or is a TextField actually technically a special kind of Button, somehow?

   

should a read directly a button

   

@brendan asks if there's a typo in the article?

it could be directly on the button, but it can be anywhere else in the view...

Please note the article was transcribed from the video. If you rewatch the video he's flying through an example and probably misspoke. I agree with you, he probably meant to say you can add the .onSubmit() to the TextField. But don't sleep on the advice that the .onSubmit() can also be attached to other structures in the view hierarchy.

Experimentation will show that you can attach onSubmit() to some structures, but not all of them. Also, it's important to know that when you attach .onSubmit() to several structures, SwiftUI will fire the ones higher in the structure tree first.

Here's some modifications to the lesson's code. I urge you to paste this into your project and experiment. There are several places in the code below where I've added .onSubmit to the view's structures. Comment and uncomment each one, then run the application. The addNewWord() function will print a comment showing where it was called.

struct WordListView: View {
    @State private var newWord = "mule"
    let rootWord               = "cromulent"
    @State private var usedWords: [String] = []
    var body: some View {
        NavigationStack {
            List {
                Section{
                    TextField("Guess", text: $newWord)
                        .onSubmit { addNewWord(from: "TextField") }  // ๐Ÿ‘ˆ๐Ÿผ comment out to experiment
                }
                .onSubmit { addNewWord(from: "Section 1") } // ๐Ÿ‘ˆ๐Ÿผ comment out to experiment

                 Section { ForEach(usedWords, id: \.self) { Text($0) } }
                // .onSubmit { addNewWord(from: "Section 2") }  // ๐Ÿ‘ˆ๐Ÿผ does not work

                Section {
                    Text("Rule #1: Keep Coding")
                    Text("Rule #2: Have Fun")
                    Button("Add Word") {  } // do nothing
                    // .onSubmit { addNewWord(from: "Button") } // ๐Ÿ‘ˆ๐Ÿผ does not work
                }

            }
            .navigationTitle(rootWord)
            // .onSubmit { addNewWord(from: "List") }  // ๐Ÿ‘ˆ๐Ÿผ comment out to experiment
        }

    }
    //               ๐Ÿ‘‡๐Ÿผ   add for debugging
    func addNewWord(from: String) {
        let answer = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
        guard answer.count > 0 else { return }
        usedWords.insert(answer, at: 0)
        newWord = ""
        print("from \(from)") // ๐Ÿ‘ˆ๐Ÿผ  for debugging!
    }
}

Keep Coding

This is a great lesson in experimentation!

   

@obelix is there some way we can notify Paul, so he can consider if he wants to tweak the text on the page?

I realise he's unlikely to want to re-do the video (or even part of the video) but perhaps he might want to tweak the text a little?

   

Note - there's a handy guide on how to email Paul, so I'll do that.

   

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developerโ€™s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topicโ€ฆ

You need to create an account or log in to reply.

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.