Using PropertyWrapper to utilize UserDefaults as a variable
- Swift 5.0
- iOS 11.0+
- macOS 10.13+
- watchOS 4.0+
You can use The Swift Package Manager to install UserDefaultsKey by adding the proper description to your Package.swift file:
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.package(url: "https://github.com/devhplusn/UserDefaultsKey.git", from: "1.0.0")
]
)
UserDefaultsKey is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'UserDefaultsKey', :tag => '1.0.0', :git => 'https://github.com/devhplusn/UserDefaultsKey'
import UserDefaultsKey
enum Gender: String, Codable {
case male = "male"
case female = "female"
}
class User: Codable {
var age: Int
var name: String
var gender: Gender
init(age: Int, name: String, gender: Gender) {
self.age = age
self.name = name
self.gender = gender
}
}
Example
@UserDefaults.Key("CURRENT_USER")
var current: User?
current = User(age: 30, name: "Some one", gender: .male)
print(current)
// Prints Optional User (age: 30, name: Some one, gender: Gender.male)
These works are available under the MIT license. See the LICENSE file for more info.