Skip to content

Instantly share code, notes, and snippets.

@gorozco58
Last active November 8, 2016 20:07
Show Gist options
  • Save gorozco58/2edc8be8a32d5a3e4cfc5bb62217f04c to your computer and use it in GitHub Desktop.
Save gorozco58/2edc8be8a32d5a3e4cfc5bb62217f04c to your computer and use it in GitHub Desktop.
import Foundation
protocol ValueRepresentable: RawRepresentable {
func value() -> String
}
extension ValueRepresentable {
func namespaced<T: RawRepresentable>(_ key: T) -> String {
return "\(Self.self).\(key.rawValue)"
}
func value() -> String {
return namespaced(self)
}
}
enum CodingKey: String, ValueRepresentable {
//here you have the properties you can code
case isUserLoggedIn
case userIdentifier
}
extension NSCoder {
//this is an example to encode a bool
func encode(_ bool: Bool, forKey key: CodingKey) {
encode(bool, forKey: key.value())
}
func decodeBool(forKey key: CodingKey) -> Bool {
print(key.value())
return decodeBool(forKey: key.value())
}
//do the same with other types
}
class Test: NSCoding {
var isUserLoggedIn = false
required init?(coder aDecoder: NSCoder) {
isUserLoggedIn = aDecoder.decodeBool(forKey: .isUserLoggedIn)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(true, forKey: .isUserLoggedIn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment