Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jon889/12b34db6303891283d695adf0e19d6ff to your computer and use it in GitHub Desktop.
Save Jon889/12b34db6303891283d695adf0e19d6ff to your computer and use it in GitHub Desktop.
RawRepresentable, meet NSCoding
enum Status: Int {
case Good
case Bad
}
// Status.RawValue == Int not NSData, therefore this extension should not be applied to status
extension RawRepresentable where RawValue == NSData, 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