userdefaults code example

Example 1: save date to userdefaults swift

//save as Date
UserDefaults.standard.set(Date(), forKey: key)

//read
let date = UserDefaults.standard.object(forKey: key) as! Date
let df = DateFormatter()
df.dateFormat = "dd/MM/yyyy HH:mm"
print(df.string(from: date))

Example 2: userdefault where

var path: [AnyObject] = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true) as [AnyObject]
    let folder: String = path[0] as! String
    NSLog("Your NSUserDefaults are stored in this folder: %@/Preferences", folder)