Realm: Results als List

前端 未结 3 1069
小蘑菇
小蘑菇 2021-02-13 22:17

Is it possible to convert Results to List or shouldn\'t I do this?

In my case I have method that has List as a parameter. I w

3条回答
  •  渐次进展
    2021-02-13 22:35

    We can use extensions to make life easier :

    extension Realm {
        func list(_ type: T.Type) -> List {
            let objects = self.objects(type)
            let list = objects.reduce(List()) { list, element -> List in
                list.append(element)
                return list
            }
            
            return list
        }
    }
    

    Usage:

    let objects = realm.list(YourObject.self)
    

提交回复
热议问题