Skip to content

Instantly share code, notes, and snippets.

@KoCMoHaBTa
Created July 17, 2017 12:22
Show Gist options
  • Save KoCMoHaBTa/78b58cbc920a90faa169ec3c54594d25 to your computer and use it in GitHub Desktop.
Save KoCMoHaBTa/78b58cbc920a90faa169ec3c54594d25 to your computer and use it in GitHub Desktop.
//
// UserDefaults+Date.swift
// https://gist.github.com/KoCMoHaBTa/78b58cbc920a90faa169ec3c54594d25
//
// Created by Milen Halachev on 7/17/17.
// Copyright © 2017 Milen Halachev. All rights reserved.
//
import Foundation
extension UserDefaults {
func date(forKey key: String, using formatter: DateFormatter) -> Date? {
guard let string = self.string(forKey: key) else {
return nil
}
let value = formatter.date(from: string)
return value
}
func set(_ value: Date?, forKey key: String, using formatter: DateFormatter) {
var string: String? = nil
if let date = value {
string = formatter.string(from: date)
}
self.set(string, forKey: key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment