Created
February 12, 2016 17:58
-
-
Save mingsai/517a8b0cbaf7819ad366 to your computer and use it in GitHub Desktop.
A swift extension showing how one can add UIColor objects to NSUserDefaults.
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
| // | |
| // MNGUserDefaultsExtensions.swift | |
| // | |
| // | |
| // Created by Tommie N. Carter, Jr., MBA on 11/4/15. | |
| // Copyright © 2015 MING Technology. All rights reserved. | |
| // | |
| import Foundation | |
| import UIKit | |
| 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