问题
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
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?
- make dataSource as a
Listand convert theResultsobject toList? How?? - make dataSource as a
Resultsand convert theListtoResults? How?? - Or may be you have a better way of doing it, Please share it with me.
Thanks,
回答1:
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)
回答2:
I have found A simple way to convert List to Results making use if the filter method, it always returns Results object. Just gave it a true predicate.
dataSource = aRealmObject.someList.filter("TRUEPREDICATE") //this is a Results object.
来源:https://stackoverflow.com/questions/37184174/how-to-convert-realmswift-list-to-results