代码之家  ›  专栏  ›  技术社区  ›  AresProductions

未硬键入参数后,用户默认值观察器停止工作

  •  -1
  • AresProductions  · 技术社区  · 7 年前

    在我将forkeyPath参数替换为字符串结构而不是硬输入之前,我的用户默认观察器一直工作得很好。

    它以前是如何工作的:

    UserDefaults.standard.addObserver(self, forKeyPath: "packagename.fontSize", options: .new, context: nil)
    

    现在情况如何(不再工作)

    UserDefaults.standard.addObserver(self, forKeyPath: UserSavedSettings.FontSize, options: .new, context: nil)
    

    参数改变后,功能:

    observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
    

    不再呼叫…

    这是我的usersavedsettings.fontsize声明

    struct UserSavedSettings {
        static let FontSize = "packagename.fontSize"
        static let FontType = "packagename.fontType"
        static let Theme = "packagename.theme"
    }
    

    我能怎么解决这个问题吗?提前谢谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   AresProductions    7 年前

    struct UserSavedSettings {
        static let FontSize = "packagename_fontSize" // replaced . with _
        static let FontType = "packagename_fontType"
        static let Theme = "packagename_theme"
    }