Skip to content

Instantly share code, notes, and snippets.

@ctrevarthen
Last active November 19, 2015 03:40
Show Gist options
  • Save ctrevarthen/f230f5b1603f4c8e97f2 to your computer and use it in GitHub Desktop.
Save ctrevarthen/f230f5b1603f4c8e97f2 to your computer and use it in GitHub Desktop.
Encoding and Decoding for NSUserDefaults
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