Chapters 6, 7, and 17 use a version of the .onChange(of:perform) modifier which was deprecated in macOS 14

This is hardly the end of the world, but while walking through macOS Apprentice’s content today, I discovered that chapter 6 twice calls for the use of the .onChange(of:perform) modifier. I’m using Xcode 15.4 and, when using that version, Apple warns that the form of .onChange(of:perform) as presented in the book has been deprecated. The warning I receive is:

‘onChange(of:perform:)’ was deprecated in macOS 14.0: Use onChange with a two or zero parameter action closure instead.

In the case of Chapter 6’s sample code, which is within the GuessesView.swift file, I believe that can be changed as follows to eliminate the warning. Note that the code as shown in the book has been commented out (i.e., double slashes at the start of the line) and my replacement code, which immediately follows, is surrounded by double asterisks (I thought the editor would honor my boldface request, but that does not seem to be the case):

struct GuessesView: View {
            .
            .
            .    
            LabeledContent("Guess a letter:") {
                TextField("", text: $nextGuess)
                    .frame(width: 50)
                    .textFieldStyle(.roundedBorder)
                    .disabled(game.gameStatus != .inProgress)
//                    .onChange(of: nextGuess) { newValue in
                    **.onChange(of: nextGuess) { oldvalue, newValue in**
                        game.processGuess(letter: newValue)
                        nextGuess = ""
                    }
//                    .onChange(of: game.gameStatus) { _ in
                    **.onChange(of: game.gameStatus) {**
                        EntryFieldHasFocus = true
                    }
                    .focused($EntryFieldHasFocus)
            }
            .
            .
            .
}

Looking ahead, I can see that the .onChange(of:perform) modifier is also discussed in chapters 7 and 14. Given that this is a beginner’s book, readers are likely to find it rather worrisome when warnings and errors occur in their code (that are taken verbatim from the book) that are not discussed in the text proper. Therefore, I would hope that future editions of the book, whether print or digital, discuss the warning or eliminate it by updating the code.

Thanks for the report. You are quite correct about the new format for onChange. This book was written using Xcode 14 (macOS 13), but Xcode 15 (macOS 14) introduced the new style for this modifier.

Kodeco no longer publishes books, but they have transferred the rights of macOS by Tutorials to me and I am hoping the same will happen with this book when it comes out-of-catalog later this year.

If that happens, I will update the book for Xcode 16 and macOS 15.

1 Like