Created
September 1, 2025 09:32
-
-
Save erdemildiz/2886a496aaf7d4eb5840d5ecb9030947 to your computer and use it in GitHub Desktop.
Propery Wrapper with Optional Value Support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Source: https://www.avanderlee.com/swift/property-wrappers/ | |
| @propertyWrapper | |
| struct UserDefault<Value> { | |
| let key: String | |
| let defaultValue: Value | |
| var container: UserDefaults = .standard | |
| var wrappedValue: Value { | |
| get { | |
| return container.object(forKey: key) as? Value ?? defaultValue | |
| } | |
| set { | |
| // Check whether we're dealing with an optional and remove the object if the new value is nil. | |
| if let optional = newValue as? AnyOptional, optional.isNil { | |
| container.removeObject(forKey: key) | |
| } else { | |
| container.set(newValue, forKey: key) | |
| } | |
| } | |
| } | |
| var projectedValue: Bool { | |
| return true | |
| } | |
| } | |
| /// Allows to match for optionals with generics that are defined as non-optional. | |
| public protocol AnyOptional { | |
| /// Returns `true` if `nil`, otherwise `false`. | |
| var isNil: Bool { get } | |
| } | |
| extension Optional: AnyOptional { | |
| public var isNil: Bool { self == nil } | |
| } | |
| extension UserDefault where Value: ExpressibleByNilLiteral { | |
| /// Creates a new User Defaults property wrapper for the given key. | |
| /// - Parameters: | |
| /// - key: The key to use with the user defaults store. | |
| init(key: String, _ container: UserDefaults = .standard) { | |
| self.init(key: key, defaultValue: nil, container: container) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage