42 lines
1.1 KiB
Swift
42 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
extension UserDefaults {
|
|
func _get<Value: Defaults.Serializable>(_ key: String) -> Value? {
|
|
guard let anyObject = object(forKey: key) else {
|
|
return nil
|
|
}
|
|
|
|
return Value.toValue(anyObject)
|
|
}
|
|
|
|
func _set<Value: Defaults.Serializable>(_ key: String, to value: Value) {
|
|
if (value as? _DefaultsOptionalProtocol)?._defaults_isNil == true {
|
|
removeObject(forKey: key)
|
|
return
|
|
}
|
|
|
|
set(Value.toSerializable(value), forKey: key)
|
|
}
|
|
|
|
public subscript<Value: Defaults.Serializable>(key: Defaults.Key<Value>) -> Value {
|
|
get { _get(key.name) ?? key.defaultValue }
|
|
set {
|
|
_set(key.name, to: newValue)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UserDefaults {
|
|
/**
|
|
Remove all entries.
|
|
|
|
- Note: This only removes user-defined entries. System-defined entries will remain.
|
|
*/
|
|
public func removeAll() {
|
|
// We're not using `.removePersistentDomain(forName:)` as it requires knowing the suite name and also because it doesn't emit change events for each key, but rather just `UserDefaults.didChangeNotification`, which we don't subscribe to.
|
|
for key in dictionaryRepresentation().keys {
|
|
removeObject(forKey: key)
|
|
}
|
|
}
|
|
}
|