mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
41 lines
756 B
Swift
41 lines
756 B
Swift
import Foundation
|
|
|
|
// MARK: - Cachable
|
|
|
|
/**
|
|
Implementation of Cachable protocol.
|
|
*/
|
|
extension Date: Cachable {
|
|
public typealias CacheType = Date
|
|
|
|
/**
|
|
Creates an instance from Data.
|
|
- Parameter data: Data to decode from
|
|
- Returns: An optional CacheType
|
|
*/
|
|
public static func decode(_ data: Data) -> CacheType? {
|
|
return NSKeyedUnarchiver.unarchiveObject(with: data) as? Date
|
|
}
|
|
|
|
/**
|
|
Encodes an instance to Data.
|
|
- Returns: Optional Data
|
|
*/
|
|
public func encode() -> Data? {
|
|
return NSKeyedArchiver.archivedData(withRootObject: self)
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
/**
|
|
Helper NSDate extension.
|
|
*/
|
|
extension Date {
|
|
|
|
/// Checks if the date is in the past.
|
|
var inThePast: Bool {
|
|
return timeIntervalSinceNow < 0
|
|
}
|
|
}
|