Skip to content

Instantly share code, notes, and snippets.

@davidahouse
Created July 22, 2015 17:13
Show Gist options
  • Save davidahouse/3eb07164679ce5fdf2d7 to your computer and use it in GitHub Desktop.
Save davidahouse/3eb07164679ce5fdf2d7 to your computer and use it in GitHub Desktop.
Simple NSCoding with Swift example, using enum & rawValue for the key names.
@objc(Person)
final public class Person : NSCoding {
public var firstName:String
public var lastName:String
private enum SerializationKeys : String {
case FirstName
case LastName
}
init(firstName:String,lastName:String) {
self.firstName = firstName
self.lastName = lastName
}
// MARK: NSCoding
public required init(coder aDecoder: NSCoder) {
title = aDecoder.decodeObjectForKey(SerializationKeys.FirstName.rawValue) as! String
number = aDecoder.decodeObjectForKey(SerializationKeys.LastName.rawValue) as! String
}
public func encodeWithCoder(encoder: NSCoder) {
encoder.encodeObject(firstName, forKey: SerializationKeys.FirstName.rawValue)
encoder.encodeInteger(lastName, forKey: SerializationKeys.LastName.rawValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment