I want to save a variable defined from a dropdown in the preferences sheet to screensaverdefaults. Bools seem to save properly, but strings don't seem to save properly.
i tried Database.standard.set(selectedVideo: "Choice")
where choice is a string. But something just doens't work right. I can upload the entire project if it can help me, i'm quite new to swift development.
Thanks in advance!
Added gists to some of the code: https://gist.github.com/MaxTechnics/394bb98e88f526de5b93b50efdfa4bcf https://gist.github.com/MaxTechnics/5527ede328595a63049e88ffa8f9c5f2
There is a mistake in getter selectedVideo
:
var selectedVideo: String {
return String(Database.Key.selectedVideo)
}
This code just returns Key
, not value.
It's necessary to use string accessor UserDefaults.string(forKey:)
:
func string(forKey defaultName: String) -> String?
So the correct code should look like this:
var selectedVideo: String? {
return string(forKey: Database.Key.selectedVideo)
}
or this:
var selectedVideo: String {
return string(forKey: Database.Key.selectedVideo) ?? "default value"
}