RealmSwift无法转换结果.到Results< Object> [英] RealmSwift Cannot cast Results<SomeOjbect> to Results<Object>

查看:153
本文介绍了RealmSwift无法转换结果.到Results< Object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RealmSwift版本:最新的master分支

RealmSwift version: latest master branch

所以我有一个Realm对象,如:

So I have a Realm Object like:

import RealmSwift
class SomeObject: Object
{
    @objc dynamic var datetime = ""
    @objc dynamic var city = 0

    convenience init(city: Int, datetime: String)
    {
        self.init()
        self.city = city
        self.datetime = datetime
    }
}

有一个类似

static func createlineData(from results: Results<Object>?) -> LineChartData

现在,我获取一些结果并传递给createLineData:

Now I fetch some results and pass to createLineData:

let realm = try! Realm()
let results = realm.objects(SomeObject.self).filter("city = \(city.rawValue)")
let lineData = createlineData(from: results as? Results<Object>)

编译器警告我,类型转换将始终失败:

compiler warns me that the type cast will always fail:

从Results<"SomeObject">转换为不相关的类型Results<"Object">总是失败

Cast from Results<"SomeObject"> to unrelated type Results<"Object"> always fails

我很困惑,因为SomeObject只是一个子类.我该如何解决?预先感谢.

I am confused since SomeObject is just a subclass. How can I fix it? Thanks in advance.

更新:

我想做的是

static func createlineData(from results: Results<Object>?) -> LineChartData

永远都不能更改,因此我需要基于枚举的city进行查询过滤,将其传递到createlineData(from results: Results<Object>?)中,并稍后在createlineData中从datetime >

can never be changed, so I need to make a query to filt based on city which is enum, pass them into createlineData(from results: Results<Object>?), and access other properties like datetime later in createlineData, from Results<Object>

推荐答案

在Swift中,每个泛型类都表示自己的类型,即使您有一个泛型类,其中泛型类型参数是您的其他泛型类的子类,作为父类的泛型参数,这两个泛型类不会通过继承进行关联.

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.

这就是为什么即使SomeObjectObject的子类,也无法将Results<SomeObject>强制转换为Results<Object>的原因.

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

此外,Realm中的Results类型是同质集合类型,因此您不能在同一Results对象中存储Object的不同子类,因此强制转换为Results<Object>还是没有意义的.如果需要将来自不同Realm模型类的对象存储在同一集合中,则需要牺牲Results的自更新特性,并坚持将对象存储在例如Array中.

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无法转换结果.到Results&lt; Object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆