Last active
May 19, 2020 16:36
-
-
Save Salarsoleimani/82cc67fa9f8535e24e51815da886761d to your computer and use it in GitHub Desktop.
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
// | |
// UserDefaults.swift | |
// | |
// Created by Salar Soleimani on 2020-04-14. | |
// Copyright © 2020 SaSApps. All rights reserved. | |
// | |
import Foundation | |
@propertyWrapper | |
struct UserDefault<T> { | |
let key: String | |
let defaultValue: T | |
init(_ key: String, defaultValue: T) { | |
self.key = key | |
self.defaultValue = defaultValue | |
} | |
var wrappedValue: T { | |
get { | |
return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue | |
} | |
set { | |
UserDefaults.standard.set(newValue, forKey: key) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
And simply In the code: