162
votes

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?

11

11 Answers

406
votes

Weird, the answer is very straightforward. Here is how I do it:

let array = Array(results) // la fin
32
votes

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).

21
votes

I found a solution. Created extension on Results.

extension Results {
    func toArray<T>(ofType: T.Type) -> [T] {
        var array = [T]()
        for i in 0 ..< count {
            if let result = self[i] as? T {
                array.append(result)
            }
        }

        return array
    }
}

and using like

class func getSomeObject() -> [SomeObject]? {
    let objects = Realm().objects(SomeObject).toArray(SomeObject) as [SomeObject]

    return objects.count > 0 ? objects : nil
}
18
votes

With Swift 4.2 it's as simple as an extension:

extension Results {
    func toArray() -> [Element] {
      return compactMap {
        $0
      }
    }
 }

All the needed generics information is already a part of Results which we extend.

To use this:

let someModelResults: Results<SomeModel> = realm.objects(SomeModel.self)
let someModelArray: [SomeModel] = someModelResults.toArray()
8
votes

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<T>(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<T>(type: T.Type) -> [T] {
        return compactMap { $0 as? T }
    }
}
6
votes

Swift 3

extension Results {
    func toArray<T>(ofType: T.Type) -> [T] {
        var array = [T]()
        for i in 0 ..< count {
            if let result = self[i] as? T {
                array.append(result)
            }
        }

        return array
    }
}

Usage

class func getSomeObject() -> [SomeObject]? {
   let defaultRealm = try! Realm()
    let objects = defaultRealm.objects(SomeObject.self).toArray(ofType : SomeObject.self) as [SomeObject]

    return objects.count > 0 ? objects : nil
}

Alternative : Using generics

class func getSomeObject() -> [T]? {
        let objects = Realm().objects(T.self as! Object.Type).toArray(ofType : T.self) as [T]

        return objects.count > 0 ? objects : nil
}
4
votes

it's not a good idea to convert Results to Array, because Results is lazy. But if you need try this:

func toArray<T>(ofType: T.Type) -> [T] {
    return flatMap { $0 as? T }
}

but better way is to pass Results wherever you need. Also you can convert Results to List instead of Array.

List(realm.objects(class))

if the first func is not working you can try this one:

var refrenceBook:[RefrenceProtocol] = []
let faceTypes = Array(realm.objects(FaceType))
refrenceBook = faceTypes.map({$0 as FaceType})
3
votes

Solution for Swift 4, Realm 3

extension Results {
    func toArray<T>(ofType: T.Type) -> [T] {
        let array = Array(self) as! [T]
        return array
    }
}

Now converting can be done as below

let array = Realm().objects(SomeClass).toArray(ofType: SomeClass.self)
2
votes

I'm not sure, if there is any efficient way to do this.

But you can do it by create a Swift array and append it in the loop.

class func getSomeObject() -> [SomeObject]? {
    var someObjects: [SomeObject] = []
    let objects = Realm().objects(SomeObject)
    for object in objects{
        someObjects += [object]
    }
    return objects.count > 0 ? someObjects : nil
}

If you feel it's too slow. I recommend you to pass around Realm Results object directly.

2
votes
extension Results {
    var array: [Element]? {
        return self.count > 0 ? self.map { $0 } : nil
    }
}

So, you can use like:

Realm().objects(SomeClass.self).filter("someKey ENDSWITH %@", "sth").array
2
votes
extension Results {
    func materialize() -> [Element] {
        return Array(self)
    }
}