Forked from joshavant/RawRepresentableExtensions.swift
Last active
April 21, 2016 22:54
-
-
Save Jon889/12b34db6303891283d695adf0e19d6ff to your computer and use it in GitHub Desktop.
RawRepresentable, meet NSCoding
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
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