What I want to implement:
class func getSomeObject() -> [SomeObject]? {
let objects = Realm().objects(SomeObject)
return objects.count > 0 ? o
This an another way of converting Results
into Array with an extension with Swift 3 in a single line.
extension Results {
func toArray() -> [T] {
return self.map { $0 }
}
}
For Swift 4 and Xcode 9.2
extension Results {
func toArray(type: T.Type) -> [T] {
return flatMap { $0 as? T }
}
}
With Xcode 10 flatMap
is deprecated you can use compactMap
for mapping.
extension Results {
func toArray(type: T.Type) -> [T] {
return compactMap { $0 as? T }
}
}