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
?
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
?
Weird, the answer is very straight forward. Here is how I do it:
let array = Array(results) // la fin
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).
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 }
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 }
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.
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})
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)
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
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 } } }