My UserDefault observer used to work fine until I replaced my forKeyPath parameter to a string Struct instead of hardtyping it.
How it used to be when it was working:
UserDefaults.standard.addObserver(self, forKeyPath: "packagename.fontSize", options: .new, context: nil)
How it is now (not working anymore)
UserDefaults.standard.addObserver(self, forKeyPath: UserSavedSettings.FontSize, options: .new, context: nil)
After the parameter change the function:
observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
is not called anymore...
Here is my UserSavedSettings.FontSize declaration
struct UserSavedSettings {
static let FontSize = "packagename.fontSize"
static let FontType = "packagename.fontType"
static let Theme = "packagename.theme"
}
Any ideas how can I fix this? Thank you in advance!
Well it seems that when I remove the dots on my UserSavedSettings strings it works fine.
struct UserSavedSettings {
static let FontSize = "packagename_fontSize" // replaced . with _
static let FontType = "packagename_fontType"
static let Theme = "packagename_theme"
}
Something must be going wrong with UserDefaults when the string contains dots.