Is it possible to use enums in RealmSwift?
Asked Answered
D

4

6

I want to do something like this:

enum WeekDay {
    case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

class Person: Object {

    dynamic var birthday: WeekDay? = .Monday
    dynamic var id: String? = nil
    dynamic var birthdayRaw: String? = nil

    override static func primaryKey() -> String? {
        return "id"
    }
}

But, I'm getting an error:

Property cannot be marked dynamic because its type cannot be represented in Objective-C

How can I solve this ? Thanks for any help.

Daune answered 5/5, 2016 at 12:27 Comment(0)
D
11

Realm doesn't have a direct way do it. Github issue.

But you can consider this trick

enum WeekDay: String {
    case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

class Person: Object {
    private var _day: WeekDay?
    var birthday: WeekDay? {
        get {
            if let resolTypeRaw = birthdayRaw  {
                _day = WeekDay(rawValue: resolTypeRaw)
                return _day
            }
            return .Sunday
        }
        set {
            birthdayRaw = newValue?.rawValue
            _day = newValue
        }
    }

    dynamic var id: String? = nil
    dynamic var birthdayRaw: String? = nil

    override static func primaryKey() -> String? {
        return "id"
    }
}
Daune answered 5/5, 2016 at 12:27 Comment(2)
Or you can mark the enum type definition with @objc if it is an Int enum.Enclasp
Also, you would not be able to use the birthDay in a query, which would make it confusing. You would have to query using _day.Isolt
T
4

As of Realm 3.x you can use Int-based enums (apparently, by side-effect).

As of Realm 4.1 you can use any RawRepresentable enum (Int, Float, String) by complying with the "RealmEnum" protocol. Details in the pull request

Tiptop answered 3/12, 2019 at 15:34 Comment(1)
Update to this: You should be using PersistableEnum or you won't be able to use @Persistable. PersistableEnum uses RealmEnum internally, as well as other protocols. With that, you should have persistable Enums without the need for the extensions mentioned in other entriesForjudge
P
1

i've create an extension, i hope it will help you

import RealmSwift

protocol RealmPersistableEnum: RawRepresentable, _OptionalPersistable { }

extension RealmPersistableEnum where RawValue: _OptionalPersistable {
    static func _rlmGetProperty(_ obj: ObjectBase, _ key: PropertyKey) -> Self {
        Self(rawValue: RawValue._rlmGetProperty(obj, key)) ?? Self()
    }

    static func _rlmGetPropertyOptional(_ obj: ObjectBase, _ key: PropertyKey) -> Self? {
        guard let value = RawValue._rlmGetPropertyOptional(obj, key) else { return nil }
        return Self(rawValue: value)
    }
    
    static func _rlmSetProperty(_ obj: ObjectBase, _ key: PropertyKey, _ value: Self) {
        RawValue._rlmSetProperty(obj, key, value.rawValue)
    }
}

Use example

enum SomeEnumInt: Int, RealmPersistableEnum {
    case none = 0
    case test = 1
    case debug = 2

    init() {
        self = .none
    }
}

enum SomeEnumString: String, RealmPersistableEnum {
    case none
    case test
    case debug

    init() {
        self = .none
    }
}

class Foo: Object {
    @Persisted var v1: String
    @Persisted var v2: SomeEnumInt
    @Persisted var v3: SomeEnumString
}
Pernick answered 14/9, 2021 at 11:59 Comment(0)
S
0
enum QuestIcon: String, RawRepresentable, CaseIterable {
    case tray = "tray.2.fill"
    case charBook = "character.book.closed.fill"
}

extension QuestIcon: PersistableEnum  { } // THIS!!!!!!!
Selfmoving answered 13/2, 2024 at 2:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.