Last active
November 19, 2015 03:40
-
-
Save ctrevarthen/f230f5b1603f4c8e97f2 to your computer and use it in GitHub Desktop.
Encoding and Decoding for 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
| class Product : NSObject, NSCoding { | |
| let nameKey = "name" | |
| let qtyKey = "qty" | |
| var name : String = "" | |
| var qty : Int = 0 | |
| required init(coder aDecoder: NSCoder) { | |
| if let name = aDecoder.decodeObjectForKey(nameKey) as? String { | |
| self.name = name | |
| } | |
| if let qty = aDecoder.decodeObjectForKey(qtyKey) as? Int { | |
| self.qty = qty | |
| } | |
| } | |
| func encodeWithCoder(aCoder: NSCoder) { | |
| aCoder.encodeObject(self.name, forKey: nameKey) | |
| aCoder.encodeObject(self.qty, forKey: qtyKey) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment