如何将RealmSwift List转换为结果? [英] how to convert RealmSwift List to Results?

查看:492
本文介绍了如何将RealmSwift List转换为结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Realm List / Results 作为 UITableView 的数据源。在某些时候,我为它分配一个列表。喜欢:

I am using a Realm List/Results as my dataSource for a UITableView. At some point I assign a list to it. like:

var dataSource:List<SomeObject>! // Or >> Results<SomeObject>!
let aRealmObject =  realm.objectForPrimaryKey(SomeObject.self, key: objectId) 
dataSource = aRealmObject.someList // dataSource should be List

然后我在此列表上有一个过滤器如果用户更改过滤日期,我会这样:

Then I have a filter on this list If the user changed the filter dates, I do like this:

dataSource = dataSource.filter("FILTER THE DATES",newDates) // dataSource should be Results

但是上面的行会导致错误,因为过滤器的返回类型是结果对象和 aRealmObject.someList 是一个列表。

But the line above causes an error as the return type of filter is a Results object and aRealmObject.someList is a List.

处理这种情况的最佳方法是什么?

What is the best way to deal with this situation?


  • 将dataSource设为列表并转换结果对象列表?怎么??

  • 将dataSource作为结果并将 List 转换为结果?怎么样??

  • 或者你可能有更好的方法,请与我分享。

  • make dataSource as a List and convert the Results object to List? How??
  • make dataSource as a Results and convert the List to Results? How??
  • Or may be you have a better way of doing it, Please share it with me.

谢谢,

推荐答案

列表结果(以及 LinkingObjects )可以转换为 AnyRealmCollection 类型。我认为这可能是标准化所有Realm的数组类型类型的最佳方法:

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)

这篇关于如何将RealmSwift List转换为结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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