-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Swift enums with associated values #2755
Comments
Just from my own experience with Realm but the developers will probably be able to answer this better. This depends on what |
They are not all the same Realm object, otherwise it would be:
I see that there are still some limitations. The problem I have with this:
Is that they have nothing to do with each other. |
Well in order for a List to hold objects they have to be of the same type. The only workaround I can think of without creating a base class is to have a List for each |
What @tom-sparo suggests here, would not work:
That's because polymorphism isn't supported yet. You can just store objects of a specific type. enum FooBar {
case Bar1(Foo1)
case Bar2(Foo2)
case Bar3(Foo3)
}
// where Foo1, Foo2, Foo3 are either RealmSwift.Object subclasses or supported primitives.
// A wrapper object for the FooBar enum, able to store associated values of every single case.
class FooBarBox : Object {
var fooBar: FooBar? {
get {
// Determine object type by the
if let bar1 = _bar1 {
return .Bar1(bar1)
} else if let bar2 = _bar2 {
return .Bar2(bar2)
} else if let bar3 = _bar3 {
return .Bar3(bar3)
} else {
return nil
}
}
didSet {
switch newValue {
case Bar1(let x):
_bar1 = x
case Bar2(let x):
_bar2 = x
case Bar3(let x):
_bar3 = x
}
}
}
internal dynamic var _bar1: Foo1?
internal dynamic var _bar2: Foo2?
internal dynamic var _bar3: Foo3?
}
// Note: struct's accessors can't be dynamically overridden at runtime. It'd need to be a class inheriting from RealmSwift.Object to be able to persist it.
class CollectionOfFoos : Object {
let foos = List<FooBarBox>()
} |
Wait really? Polymorphism isn't supported? In what way is it not supported? I've been using polymorphic classes as delegates for a bit and haven't been burned yet -- it seems to be working? Is this something I should expect to work only sometimes? |
Inheritance is supported by Realm, but polymorphism not yet. It is supported by Swift though. See the detailed roundup here.
|
Thanks that's very helpful! |
I will close this for now in favor of #921. Once that can be tackled, we an revisit this issue as well. Thanks for bringing this up though! |
thanks @mrackwitz |
I saw this question/answer, but how would you go with an enum with different types associated? A quick example:
You would then have something like:
How you would store this
CollectionOfFoos
. For the time being it's not aRLMObject
subclass.The text was updated successfully, but these errors were encountered: