Created
July 16, 2019 13:42
-
-
Save HassanElDesouky/373bcf4f1002f77557814a3e24fa4759 to your computer and use it in GitHub Desktop.
Store UIColor with UserDefaults
This file contains hidden or 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
| extension UserDefaults { | |
| func colorForKey(key: String) -> UIColor? { | |
| var colorReturnded: UIColor? | |
| if let colorData = data(forKey: key) { | |
| do { | |
| if let color = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(colorData) as? UIColor { | |
| colorReturnded = color | |
| } | |
| } catch { | |
| print("Error UserDefaults") | |
| } | |
| } | |
| return colorReturnded | |
| } | |
| func setColor(color: UIColor?, forKey key: String) { | |
| var colorData: NSData? | |
| if let color = color { | |
| do { | |
| let data = try NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false) as NSData? | |
| colorData = data | |
| } catch { | |
| print("Error UserDefaults") | |
| } | |
| } | |
| set(colorData, forKey: key) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment