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.