Created
October 6, 2017 17:39
-
-
Save xiangyu-sun/8a37b37208a13a67283611200afb9b8d 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
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