RealmSwift: Convert Results to Swift Array
What I want to implement:
class func getSomeObject() -> [SomeObject]? {
let objects = Realm().objects(SomeObject)
return objects.count > 0 ? objects : nil
}
How can I return object as [SomeObject]
instead if Results
?
Solution 1:
Weird, the answer is very straightforward. Here is how I do it:
let array = Array(results) // la fin
Solution 2:
If you absolutely must convert your Results
to Array
, keep in mind there's a performance and memory overhead, since Results
is lazy. But you can do it in one line, as results.map { $0 }
in swift 2.0 (or map(results) { $0 }
in 1.2).