Skip to content

Instantly share code, notes, and snippets.

@xiangyu-sun
Created October 6, 2017 17:39
Show Gist options
  • Save xiangyu-sun/8a37b37208a13a67283611200afb9b8d to your computer and use it in GitHub Desktop.
Save xiangyu-sun/8a37b37208a13a67283611200afb9b8d to your computer and use it in GitHub Desktop.
import UIKit
class EncryptionTransformer: ValueTransformer {
override class func allowsReverseTransformation() -> Bool{
return true
}
override func transformedValue(_ value: Any?) -> Any?{
guard let date = value as? Date else {return nil}
return date.stringlize().data(using: .utf8)?.base64EncodedData()
}
override func reverseTransformedValue(_ value: Any?) -> Any?{
guard let data = value as? Data, let decoded = Data(base64Encoded: data) else {return nil}
return String(data: decoded, encoding: .utf8)?.datelize()
}
}
extension Date {
func stringlize() -> String {
let formater = DateFormatter()
formater.dateFormat = "yyyyMMddss"
return formater.string(from: self)
}
}
extension String {
func datelize() -> Date? {
let formater = DateFormatter()
formater.dateFormat = "yyyyMMddss"
return formater.date(from: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment