Using enum as property of Realm model

Related searches

Is it possible to use an Enum as a property for my model? I currently have a class like this:

class Checkin: RLMObject {
  dynamic var id: Int = 0
  dynamic var kind: String = "checked_in"
  var kindEnum: Kind = .CheckedIn {
    willSet { self.kind = newValue.rawValue }

  enum Kind: String {
    case CheckedIn = "checked_in"
    case EnRoute = "en_route"
    case DroppedOff = "dropped_off"

It works okay, but I'd like to be able to have the kind property be the Enum and have Realm automatically call .rawValue on the property when it is saving an object to the store. Is this possible in Realm or is there a feature request already out there for it?

You should override your kindEnum's setter and getter for this case:

enum Kind: String {
  case CheckedIn
  case EnRoute
  case DroppedOff

class Checkin: Object {
  @objc dynamic var id = 0
  var kind = Kind.CheckedIn.rawValue
  var kindEnum: Kind {
    get {
      return Kind(rawValue: kind)!
    set {
      kind = newValue.rawValue

Persisting Swift Enumerations With | by Igor Rinkovec, How to store enums with Realm. The idea here is to provide custom getters and setters for our enum and store an atomic representation in Realm. I guess you don't understand how to assign the value of property as your defined enum.The following is a useful sample for you. public enum MyEnum { ok,cancel } public class MyClass { public readonly MyEnum ok=MyEnum.ok; public readonly MyEnum cancel=MyEnum.cancel; } Sincerely, Jason Wang

I've refined this model a little further.

enum Thing: String {
    case Thing1
    case Thing2
    case Thing3

then in my Realm class object:

class myClass : Object {
    private dynamic var privateThing = Thing.Thing1.rawValue
    var thing: Thing {
        get { return Thing(rawValue: privateThing)! }
        set { privateThing = newValue.rawValue }

This allows us to write

myClassInstance.thing = .Thing1

(storing "Thing1" into privateThing), but prevents typing of

myClassInstance.privateThing = "Thing4"

which is not a valid value so preserving data integrity.

Is it possible to have a property as an Enum? � Issue #3152 � realm , enum BillState { case Overdue case Closed case Open case Future static func Is there any other way for this to work with Realm? Thanks! Enum support in EF Core is quite extensive, in this article I’ll cover how to use an enum as a Primary Key, as well as storing the integer and string value of the enum in a column.

Since Realm support Objective-C enums and they are representable by Int you can use this:

class Checkin: Object {
  dynamic var id: Int = 0
  dynamic var kind: Kind = .checkedIn

  @objc enum Kind: Int {
    case checkedIn
    case enRoute
    case droppedOff

If you need to parse to/from String you can use a custom initializer for Kind and an toString function.

There is a discussion about this in GitHub

This works with Swift 3.0 and Realm 2.0.2

Enum PropertyType, LinkingObjects. A collection of objects linking to the model owning this property. Nullable. A nullable (optional) property. Can be combined with other values. Open Visual Studio 2012. On the File menu, point to New, and then click Project. In the left pane, click Visual C#, and then select the Console template. Enter EnumCodeFirst as the name of the project and click OK.

Diogo T's solution worked before recent updates of RealmSwift. Eventually we now have to conform to RealmEnum protocol to be able to be a managed property of Realm Object.

@objc enum MyEnum: Int, RealmEnum {

Or add below in some place:

extension MyEnum: RealmEnum { }

RealmSwift documentation for it

Enum PropertyType, A collection of objects linking to the model owning this property. Nullable. A nullable (optional) property. Can be combined with other values. Object. Related object� To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma: Example. enum Level { Low, Medium, High } You can access enum items with the dot syntax: Level myVar = Level.Medium; Console.WriteLine(myVar); Run example ».

Storing enums in Realm, This has to do with the way Realm stores and reads data from the database. The values of an enum are automatically assigned an int value starting at 0 Let's say we have a Paint class on which one of the properties is the� Enumerations (or enum types for short) are a thin language wrapper around an integral type. You might want to limit their use to when you are storing one value from a closed set of values. Classification based on sizes (small, medium, large) is a good example. Using enums for control flow or more robust abstractions can be a code smell. This

In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week. An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the

How the Realm client should validate the identity of the server for secure connections. By default, when connecting to the Realm Object Server over HTTPS, Realm will validate the server’s HTTPS certificate using the system trust store and root certificates.

  • Will be great if you could add this sample to your official examples.
  • This is not great. Why can't you do this?:… (the Martin Krenek answer)?
  • You can use @objc enums directly with Realm too, but that won't work for Swift enums with String associated values.
  • In the absence of "official" support from Realm (if it's even possible), this is very useful. Thank you.
  • You could set kind variable as private, so you could only change its value with kindEnum.
  • Update: I've edited my original code because the the = "Thing1" etc. is redundant as Swift uses a String literal equivalent of the case for its rawValue, which is even better and we can use the rawValue to set the default which is more reliable and cleaner. :-)
  • Thanks for this addition, I really like this answer. +1
  • Nice solution if you have an Int enum, but it don't see how it can work with a String instead. Kind is converted to an Objective-C enum which doesn't support custom initializers or stored properties.. ?
  • Indeed, I haven't found a solution that work with any type but Int
  • Thank you for this information. I changed my enum to conform to an int. If you need a string value you can make your own computed property in the enum and return the proper value for the different cases.
  • Note that any migrations you do involving Int enums will need to assign the raw value: obj["propertyName"] = Kind.checkedIn.rawValue