Created
July 17, 2017 12:22
-
-
Save KoCMoHaBTa/78b58cbc920a90faa169ec3c54594d25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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