Skip to content

Middleware to easily write and read from UserDefaults

Notifications You must be signed in to change notification settings

deda9/EasyUserDefaults

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

EasyUserDefaults

It's just a middleware for UserDefaults to write and read values by easy way

in default way to save the user name, we write this

let userDefaults = UserDefaults.standard

// writing the value
userDefaults.set("Guest", forKey: "UserNameKey")

// reading the value
userDefaults.value(forKey: "UserNameKey") as? String

but using the subscripts, you can create the key and use it for write or read the value

let userDefaults = UserDefaults.standard
let userNameKey = UserDefaults.Key<String>("userNameKey", default: "Guest")
	
// reading the value 
let defaultValue = userDefaults[.userNameKey]
let newValue = "Jack"

// writing the value
userDefaults[.userNameKey] = newValue

and you can add more keys like this

extension UserDefaults.Keys {   
    static let didUserSeeSplash = Key<Bool>("didUserSeeSplash", default: false)
    static let userName = Key<String>("userName", default: "Guest")
    static let couterOfArticleSeen = Key<Int>("couterOfSeen", default: 0)
    static let percentage = Key<Double>("couterOfSeen", default: 0.0)
}

About

Middleware to easily write and read from UserDefaults

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages