The code is:
@IBAction func CheckBoxTuesdayButtonPressed(_ sender: Any) {
if CheckBoxTuesday.image == UIImage(imageLiteralResourceName: "CheckmarkButtonPressed"){
CheckBoxTuesday.image = UIImage(imageLiteralResourceName: "CheckBoxButton")
}
else{
CheckBoxTuesday.image = UIImage(imageLiteralResourceName: "CheckmarkButtonPressed")
UserDefaults.standard.setValue(CIImage.self, forKey: "TuesdayChecked")
}
I tried the Userdefault line of code but it said i couldnt convert it to NSuserdefault.
tl;dr
Rather than saving the image associated with the checkbox, you should save a model object, some representation that captures which check boxes have been selected.
We often draw a distinction between the “model” (some structure that indicates which check boxes have been checked) from the “view” (which images are shown in which UI controls). So, you don’t really want to save the image in persistent storage at all, but rather some model structure that indicates which switches are at what state. Likewise, when toggling a control, you toggle the representation in your model, and then update the UI (and persistent storage) accordingly.
For example, it appears that you have switches for each of the days of the week. That might call for some model object that represented which switches are turned on and off. An
OptionSet
seems like a logical candidate:And you might have some method on this type,
Days
, to toggle a particular day on and off:Then, you might have a property to store your model object to reflect what days are checked:
And then the
@IBAction
might do something like:(Forgive my changing of the names of this function and the properties as convention is to start method and variable names with a lowercase letter. We only use uppercase letters for type names, but methods and properties always start with a lowercase letter. But use whatever method/property names you want.)
Anyway, the images
checked
andunchecked
might be defined like so:And, of course, in
viewDidLoad
, you might call a function that fetches the value and updates all the controls:So all of this cuts the Gordian knot, eliminating the very idea of storing the image in
UserDefaults
at all.That having been said, I personally would not store any model data in
UserDefaults
(not even just thisInt
raw value). I would write it to persistent storage (in iOS 17, I might use Swift Data as outlined in WWDC 2023’s Meet SwiftData). Or perhaps that’s overkill, then I might makeDays
aCodable
type and write it to a JSON file in persistent storage.Use
UserDefaults
, as shown in my code snippets above, if you really must. It is, admittedly, very convenient. But one should recognize thatUserDefaults
is intended for defaults/preferences, not for model data, and we would generally use one of these other storage mechanisms for model data.Note, my model structure,
Days
, which is anOptionSet
, is just one example of a possible model structure. There are lots of possible permutations on the idea. You might have a series of Booleans. Or you may get have some other structure to capture you model data (e.g., aSet
, anArray
, a customstruct
, etc.). But the idea is the same: Have some model type that represents the logical data. But we would not generally useUIImage
(unless the user was picking custom images from their photo library or camera).