Answers for "set bool value in userdefaults swift"

1

store bool value in nsuserdefaults swift

struct Settings {

  fileprivate struct Keys {
      static let soundKey = "soundKey"
  }

  static var sound: Bool {
      set {
          UserDefaults.standard.set(newValue, forKey: Keys.soundKey)
      }
      get {
          if UserDefaults.standard.object(forKey: Keys.soundKey) == nil {
              // Value for sound not yet set ... Setting default ...

              UserDefaults.standard.set(true, forKey: Keys.soundKey)
              return true
          }
          return UserDefaults.standard.bool(forKey: Keys.soundKey)
      }

  }
}
Posted by: Guest on March-02-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language