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
How to Catch Nsunknownkeyexception in Swift 2.2
How to Make Playground Execution Time Is as Fast as If We Run in iOS Application
Uitraitcollection Clarification
Sorting Struct Array in Swift 4
How to Change Uitextfield Color in Searchcontroller
iOS 10.3 - How to Change App Icon Programmatically
Adding a Target to a Button Programmatically Throws an Error "Unrecognized Selector Sent to Class"
Dynamic Uitablecellview Height
Uiimageview .Scaleaspectfit and Autolayout Not Working Programmatically from Swift
How to Convert a Persian Date into a Gregorian in Swift
Avcapturevideopreviewlayer Add Overlays and Capture Photo in iOS
iOS 3Des with Ecb Return Half Correct Data
Firebase Checking for Null Value (Swift)
Uialertcontroller Title and Message Not Appearing