Skip to content

Instantly share code, notes, and snippets.

@StanDimitroff
Last active February 5, 2018 11:09
Show Gist options
  • Save StanDimitroff/a0cd74749e56723eac49adb4065656fd to your computer and use it in GitHub Desktop.
Save StanDimitroff/a0cd74749e56723eac49adb4065656fd to your computer and use it in GitHub Desktop.
Using enums in Realm
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