Realmswift Cannot Cast Results<Someojbect> to Results<Object>

RealmSwift Cannot cast ResultsSomeOjbect to ResultsObject

In Swift, each generic class represents its own type and even if you have a generic class where the generic type parameter is a subclass of your other generic class having the superclass as its generic parameter, the two generic classes won't be related through inheritance.

This is why you cannot cast Results<SomeObject> to Results<Object> even though SomeObject is a subclass of Object.

Here's a simple example representing the same issue with a generic class:

class A{}
class B:A{}

class GenericClass<T> {
let val:T

init(val:T) {
self.val = val
}
}

let genericA = GenericClass<A>(val: A())
let genericB = GenericClass<B>(val: B())

let upcasted = genericB as? GenericClass<A> //warning: Cast from 'GenericClass<B>' to unrelated type 'GenericClass<A>' always fails

Moreover, the Results type in Realm is a homogenous collection type, so you cannot store different subclasses of Object in the same Results object, so casting to Results<Object> wouldn't make sense anyways. If you need to store objects from different Realm model classes in the same collection, you will need to sacrifice the self-updating nature of Results and stick with storing your objects in an Array for instance.

RealmSwift: Convert Results to Swift Array

I found a solution. Created extension on Results.

extension Results {
func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for i in 0 ..< count {
if let result = self[i] as? T {
array.append(result)
}
}

return array
}
}

and using like

class func getSomeObject() -> [SomeObject]? {
let objects = Realm().objects(SomeObject).toArray(SomeObject) as [SomeObject]

return objects.count > 0 ? objects : nil
}

How to pass realm.objects(SomeObject.self).filter() to a function that needs ResultsObject

Results<Object> does not make any sense as a type, and the methods in ChartsRealm should be generic methods which take a Results<T>.

Because in this specific case all that ChartsRealm does with the Results<Object> is use ObjectiveCSupport.convert() to get a RLMResults, an unsafeBitCast() to Results<Object> should work fine. You could also just call ObjectiveCSupport.convert() yourself and pass ChartsRealm a RLMResults rather than a Swift Results object.

Can't cast from RLMObject to a subclass of Object?

RLMObject is not a typealias for Object; they are different classes entirely that have different interfaces. It sounds like you are trying to mix the Swift and Objective-C APIs, which is not supported.

realmswift how to filter Results using bitwise operator

Realm doesn't support bitwise predicates. A list of all supported predicates can be found on our NSPredicate cheatsheet; supported predicates are marked by a pink dot.

If you need to be able to perform queries, use boolean variables instead of or in addition to the bitwise flag.

how to convert RealmSwift List to Results?

Both List and Results (as well as LinkingObjects) can be converted into an AnyRealmCollection type. I think this is probably the best way to standardize all of Realm's array-type types:

var dataSource:AnyRealmCollection!
let aRealmObject = realm.objectForPrimaryKey(SomeObject.self, key: objectId)
dataSource = AnyRealmCollection(aRealmObject.someList)


Related Topics



Leave a reply



Submit