RealmSwift: Convert Results to Swift Array

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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?

回答1:

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

let array = Array(results) // la fin 


回答2:

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



回答3:

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 } 


回答4:

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 } 


回答5:

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.



回答6:

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


回答7:

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) 


回答8:

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 


回答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 }     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!