如何将RealmSwift List转换为结果? [英] how to convert RealmSwift List to Results?
问题描述
我使用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 theResults
object toList
? How?? - make dataSource as a
Results
and convert theList
toResults
? 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屋!