RealmSwift: Convert Results to Swift Array

前端 未结 11 1858
[愿得一人]
[愿得一人] 2020-12-12 15:50

What I want to implement:

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

    return objects.count > 0 ? o         


        
11条回答
  •  长情又很酷
    2020-12-12 16:22

    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.

提交回复
热议问题