Start the App and start the timer.
While it’s counting down the 6 minutes;
- open up the preferences window.
- set the Egg Timing Setting to “for very firm hard-boiled eggs” = 15 minutes.
- press ok
- the app’s checkForResetAfterPrefsChange() warning will trigger. Press cancel.
Et Voila! Egg magically turns yellow!!
I’m very new to programming so it took me ages to work out the problem. It’s a pretty big structural problem actually.
What’s happening is when you press ok, that triggers the saveNewPrefs() function which literally the first thing it does is save the new preference!!
Only AFTER that does it send out a notification to the NotificationCenter which sets off the checkForResetAfterPrefsChange() warning.
Ah but you might think all you have to do is change the order, send out the notification then save the new preference - nope! not so fast buddy!
All the checkForResetAfterPrefsChange() warning does, is update the view’s preferences if you press ok!!
It doesn’t deal with if you press CANCEL, It doesn’t stop the saveNewPrefs function AND It even updates the view’s preferences BEFORE the preference’s values have been updated themselves!!! ← this is why when you change the preferences even normally, the Egg Timer’s TimerText won’t change until you press start (which re-executes updateDisplay).
You’ve got to have the checkForResetAfterPrefsChange() function send out a notification that will tell the saveNewPrefs() function to abort if you press cancel.
And if you don’t cancel, the saveNewPrefs() function needs to send a notification to tell the updateFromPrefs() function to execute after it has saved the new preferences, not before.
Something I really don’t get is in the Preferences.Swift file.
There’s a line that says;
UserDefaults.standard.set(newValue, forKey: “selectedTime”)
to set the selectedTime variable.
My question is what’s up with the constant newValue?
The QuickHelp tells me it’s declared as a timeInterval constant in Preferences.Swift (it’s not even mentioned anywhere else?!).
More mysteriously is the set function that (according to QuickHelp) takes in 2 inputs, a Double and a String.
But newValue is a timeInterval, not a Double?!
So as far as I can see, newValue is a constant that is never declared and is the wrong type for the function it’s being called for, yet this somehow works fine?!
If you change the name newValue to something else like oldValue, then it triggers an error exactly the way I’d expect.
But there’s no mention of any newValue constants built into the Foundation in the API reference.
In this version, I explicitly name the parameter newSelectedTime and use it as the value stored in UserDefaults.
In my original code using newValue, the type of newValue is set to TimeInterval because that is the type of selectedTime which it is setting.
But if you option click on the keyword TimeInterval, you will see that it is a typealias for Double. So it really is a Double, but using the word TimeInterval provides anyone reading the code with the extra information that this is actually a time measurement.
I hope this clarifies things, but please get back to me if it is still unclear.