176

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?

12 Answers 12

442

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

let array = Array(results) // la fin
5
  • doesn't it returns an NSArray?
    – geekay
    Nov 13, 2015 at 18:59
  • 2
    @thesummersign Realm has been changing a lot recently, but one thing is for sure: The code above returns a Swift Array constructed with the results iterator.
    – Mazyod
    Nov 13, 2015 at 19:04
  • 4
    It return nil vars of the entity (initial)
    – Nike Kov
    Sep 8, 2017 at 14:20
  • 2
    I agree with @NikKov, it seems to be returning nil vars of the entity ;(
    – Jon
    Feb 10, 2018 at 8:40
  • 3
    @Jon How are you seeing that they are nil? It seems like since they are lazy, when you look at them stopped at a debug point they appear empty but if you print them out it accesses them and shows the correct value (for me).
    – Jeremiah
    Dec 12, 2018 at 15:23
34

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

4
  • Which version of Realm? Jul 1, 2015 at 14:01
  • 37
    Isn't this conversion a necessity if you don't want to leak dependency on Realm to too many classes in your project? Oct 10, 2015 at 14:49
  • 15
    map { $0 } will return LazyMapRandomAccessCollection in Swift 3, so @Mazyod answer is better.
    – Legoless
    Sep 14, 2016 at 18:09
  • @MarcinKuptel yes that's exactly the problem I found. I've been able to abstract away the realm model by creating a struct which conforms to a protocol, and it's this protocol abstraction that I define in my signatures in my codebase. However sometimes I need to convert to an array, is there way I can have a lazy collection of my abstracted protocol so that it only converts to the struct at access time?
    – Pavan
    Sep 11, 2018 at 11:52
23

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
}
2
  • 4
    for var i = 0; i < count; i++ should be replaced with for i in 0 ..< count
    – Sal
    Jun 5, 2016 at 15:21
  • 1
    The above is very confusing way of writing the extension: extension Results { var array: [Element] { return self.map { $0 } } }
    – Giles
    Aug 14, 2018 at 10:06
21

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()
2
  • How do you use that function?
    – Yash Jain
    Aug 16, 2020 at 22:51
  • 1
    realm.objects(SomeModel.self).toArray() Aug 18, 2020 at 8:13
9

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 }
    }
}
4
  • As i am using this code in 9.2 version of XCode, it shows me Use of undeclared type 'T' Feb 20, 2018 at 7:46
  • Updated my answer, you can check it. Feb 21, 2018 at 14:26
  • For Xcode 10 and above you can use compactMap instead of flatMap to avoid the warning. Nov 16, 2018 at 9:38
  • Hi! Can you please say how to call toArray func? I need to convert Results<Int> to [Int].
    – Abrcd18
    May 2, 2021 at 11:26
6

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
}
0
4

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})
2
  • After updating RealmSwift to 3.4.0, List does not take arguments. How to convert an array to List in this case? Any idea? Apr 20, 2018 at 10:24
  • 2
    @NishuPriya here you are let myList = List<Person>() myList.append(objectsIn: realm.objects(Person.self)) Apr 20, 2018 at 12:17
3

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

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
  • I did something like that only by creating an extension on Resules instead. I have posted the code as answer. Thanks :) Jun 28, 2015 at 13:47
  • yeah. I would do that too.
    – nRewik
    Jun 28, 2015 at 13:47
2
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
0
2
extension Results {
    func materialize() -> [Element] {
        return Array(self)
    }
}
0

Using Swift 5 and RealmSwift v10.20.0

This methods works:

private func convertToArray<R>(results: Results<R>) -> [R] where R: Object {
    var arrayOfResults: [R] = []
    for result in results {
        arrayOfResults.append(result)
    }
    return arrayOfResults
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.