Last active
February 5, 2018 11:09
-
-
Save StanDimitroff/a0cd74749e56723eac49adb4065656fd to your computer and use it in GitHub Desktop.
Using enums in Realm
This file contains 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
final class SortRule: Object { | |
@objc dynamic var keyRaw: String = "" | |
@objc dynamic var typeRaw: String = "" | |
var key: SortKey { | |
if let key = SortKey.fromRaw(keyRaw) { | |
return key | |
} | |
return .name | |
} | |
var type: SortType { | |
if let type = SortType.fromRaw(typeRaw) { | |
return type | |
} | |
return .same | |
} | |
convenience init(key: SortKey, type: SortType) { | |
self.init(value: | |
[ | |
"keyRaw": key.rawValue, | |
"typeRaw": type.rawValue | |
] | |
) | |
} | |
} | |
extension RawRepresentable { | |
static func fromRaw(_ value: Self.RawValue) -> Self? { | |
return Self(rawValue: value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment