I would like to have an object that is an ordered collection of multiple different kinds of Realm Objects, like so...
public class One: Object {
dynamic var name = ""
}
public class Two: Object {
dynamic var label = ""
}
public class Listing: Object {
let onesAndTwos = List<Object>()
}
Is there an elegant way to do this?
I know I can add an Enum-like wrapper object...
public class OneOrTwo: Object {
dynamic var one: One?
dynamic var two: Two?
}
public class Listing: Object {
let onesAndTwos = List<OneOrTwo>()
}
But I'd like to avoid that indirection, if possible.
List<T>
property can't contain objects of different types.
Please learn more in docs at https://realm.io/docs/swift/latest/#relationships and https://realm.io/docs/swift/latest/#model-inheritance, that should explain why it's not possible.
So I suggest you to use composition over inheritance, basically the same way as you mentioned in your post.