Created
June 1, 2015 14:52
Swift NSUserDefaults UIColor extension
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
extension NSUserDefaults { | |
func colorForKey(key: String) -> UIColor? { | |
var color: UIColor? | |
if let colorData = dataForKey(key) { | |
color = NSKeyedUnarchiver.unarchiveObjectWithData(colorData) as? UIColor | |
} | |
return color | |
} | |
func setColor(color: UIColor?, forKey key: String) { | |
var colorData: NSData? | |
if let color = color { | |
colorData = NSKeyedArchiver.archivedDataWithRootObject(color) | |
} | |
setObject(colorData, forKey: key) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift 3 version: https://gist.github.com/D-32/d30482e7fa10b6158e69610335052752