Skip to content

Instantly share code, notes, and snippets.

@joshavant
Created April 21, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshavant/92201fc050c0f160eeec3dcce8a67faf to your computer and use it in GitHub Desktop.
Save joshavant/92201fc050c0f160eeec3dcce8a67faf to your computer and use it in GitHub Desktop.
RawRepresentable, meet NSCoding
extension RawRepresentable where Self: NSCoding {
init?(rawValue: NSData) {
if let instance = NSKeyedUnarchiver.unarchiveObjectWithData(rawValue) as? Self {
self = instance
} else {
return nil
}
}
var rawValue: NSData {
get {
return NSKeyedArchiver.archivedDataWithRootObject(self)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment