Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to save a swift array of enums? #13

Closed
martinstoyanov opened this issue Oct 12, 2019 · 8 comments
Closed

Is it possible to save a swift array of enums? #13

martinstoyanov opened this issue Oct 12, 2019 · 8 comments

Comments

@martinstoyanov
Copy link

I'm getting this error

Terminating app due to uncaught exception 'RLMException', reason: 'Property 'actions' is of type 'RLMArray' which is not a supported RLMArray object type. RLMArrays can only contain instances of RLMObject subclasses. See https://realm.io/docs/objc/latest/#to-many for more information.'

my property actions is defined as this:

var actions: [Action] = []

in a Realmable struct. Action is an enum that conforms to RealmableEnum

Any ideas?

@arturdev
Copy link
Collaborator

arturdev commented Oct 12, 2019

that's a good question... I haven't tested it, but I think it's possible.
I'll check and add this functionality.

@arturdev
Copy link
Collaborator

It seems it is hard to support saving an array of enums for now..
As a workaround, I can suggest you keep the second array of RawValue type of your enum. And add your actions property to ignoredProperties list.
f.e.

enum Action: Int {
    ....
}

struct MyStruct: Realmable {

    private var _actions: [Int] = [] 
    var actions: [Action] {
        get {
             return _actions.compactMap({Action(rawValue: $0})
        } 
        set {
            _actions = newValue.map({$0.rawValue})
        }
    }

    static func ignoredProperties() -> [String] {
        return ["actions"]
    }
    
}

@martinstoyanov
Copy link
Author

Thank you, that's exactly what I ended up doing. I hope it's supported in the future.

@bryan1anderson
Copy link

Yes this is an important piece for me. I didn't realize this wasn't a feature and got pretty frustrated trying to sift through Realm errors being thrown

@martinstoyanov
Copy link
Author

@arturdev maybe, until it's implemented, we can add a disclaimer to the Readme with a link to this issue so people don't waste time trying to work it out? It could be a pretty cryptic error and not immediately obvious it's caused by Unrealm.

arturdev added a commit that referenced this issue Mar 27, 2020
@arturdev
Copy link
Collaborator

@bryan1anderson @martinstoyanov Great news! ;)

@martinstoyanov
Copy link
Author

martinstoyanov commented Mar 27, 2020

Love it, thanks so much!!! I am using Unrealm in all my new projects now :)

@arturdev
Copy link
Collaborator

Glad to hear that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants