Created
July 22, 2015 17:13
-
-
Save davidahouse/3eb07164679ce5fdf2d7 to your computer and use it in GitHub Desktop.
Simple NSCoding with Swift example, using enum & rawValue for the key names.
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
@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