Skip to content

Using PropertyWrapper to utilize UserDefaults as a variable.

License

Notifications You must be signed in to change notification settings

devhplusn/UserDefaultsKey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UserDefaultsKey

Swift Platforms GitHub license

Using PropertyWrapper to utilize UserDefaults as a variable

Requirements

  • Swift 5.0
  • iOS 11.0+
  • macOS 10.13+
  • watchOS 4.0+

Installation

Swift Package Manager

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")
    ]
)

Cocoapods

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'

Usage

Object must be implement 'Codable' Protocol

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)

License

These works are available under the MIT license. See the LICENSE file for more info.

About

Using PropertyWrapper to utilize UserDefaults as a variable.

Resources

License

Stars

Watchers

Forks

Packages

No packages published