如何将realm.objects(SomeObject.self).filter()传递给需要Results< Object>的函数 [英] How to pass realm.objects(SomeObject.self).filter() to a function that needs Results<Object>

查看:146
本文介绍了如何将realm.objects(SomeObject.self).filter()传递给需要Results< Object>的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与 RealmSwift无法转换结果< SomeOjbect>到结果<对象>

我有一个Realm对象,如:

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
    }
}

有一个框架: https://github.com/danielgindi/ChartsRealm 来自Realm的数据并绘制图表.该框架是用Swift编写的,也可以在Objc中使用,因此它结合了RLMObjectObject类型.

There is a framework: https://github.com/danielgindi/ChartsRealm, which pull data from Realm and draw charts. The framework is written in Swift, and can be used in Objc as well, so it combines RLMObject and Object types.

它具有如下功能:

public convenience init(results: Results<Object>?, xValueField: String?, yValueField: String, label: String?)

需要Results<Object>?,但是我无法将过滤器结果作为Results<Object>类型.例如

which takes Results<Object>?, however I cannot get my filter results as Results<Object> type. e.g.

realm.objects(SomeObject.self).filter("city=0")

它是Results<SomeObject>,并且不能转换为Results<Object>,如

it's Results<SomeObject>, and cannot be converted to Results<Object>, described in RealmSwift Cannot cast Results<SomeOjbect> to Results<Object>

我该如何解决?

由于ChartsRealm框架中的演示,它只是从+ (RLMResults *)allObjectsInRealm:(RLMRealm *)realm;中的Realm中读取所有对象,但是在现实世界中,我们通常需要首先过滤结果.

Because the demo in ChartsRealm framework, it simply read all objects from Realm in + (RLMResults *)allObjectsInRealm:(RLMRealm *)realm;, but in real world, we usually need to filt the results first.

如果真的无能为力,我可以接受修改框架功能参数,提交对框架的拉取请求,只是为了使其正常工作.

If there's really nothing I can do, I could accept modifying the framework function parameters, filing pull requests for the framework, just to make it work.

推荐答案

Results<Object>作为类型没有任何意义,并且ChartsRealm中的方法应该是采用Results<T>的通用方法.

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

由于在此特定情况下,ChartsRealm对Results<Object>所做的所有操作都是使用ObjectiveCSupport.convert()获取RLMResults,因此unsafeBitCast()Results<Object>应该可以正常工作.您也可以自己调用ObjectiveCSupport.convert()并将ChartsRealm传递给RLMResults而不是Swift Results对象.

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.

这篇关于如何将realm.objects(SomeObject.self).filter()传递给需要Results&lt; Object&gt;的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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