Realm: Results<T> als List<T>

Results implements the CollectionType protocol so you could use reduce to convert it:

let results: Results<MyObject> = ...
let converted = results.reduce(List<MyObject>()) { (list, element) -> List<MyObject> in
    list.append(element)
    return list
}

You could put this code in an extension or however you like.


Results and List implement CollectionType and RealmCollectionType. The latter is a specialization of the former protocol, which allows you to efficiently use aggregation functions and filter & sort entries.

Almost no method in Realm Swift make strong assumptions about the type of the collection. They just expect a SequenceType which is a generalization of the former CollectionType. For your own method, I'd recommend to go the same way. You can reach that by declaring it like shown below.

func foo<T, S: SequenceType where S.Generator.Element == T>(objects: S) { … }

Tags:

Ios

Swift

Realm