ios RealmSwift:将结果转换为 Swift 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31100011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:37:58  来源:igfitidea点击:

RealmSwift: Convert Results to Swift Array

iosswiftrealm

提问by Sahil Kapoor

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?

我怎样才能返回对象,[SomeObject]而不是 if Results

采纳答案by Sahil Kapoor

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
}

回答by Mazyod

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

奇怪,答案很直接。这是我如何做到的:

let array = Array(results) // la fin

回答by segiddins

If you absolutely must convert your Resultsto Array, keep in mind there's a performance and memory overhead, since Resultsis lazy. But you can do it in one line, as results.map { $0 }in swift 2.0 (or map(results) { $0 }in 1.2).

如果您绝对必须将您的 转换ResultsArray,请记住有性能和内存开销,因为它Results是懒惰的。但是您可以在一行中完成,就像results.map { $0 }在 swift 2.0(或map(results) { $0 }1.2)中一样。

回答by NeverwinterMoon

With Swift 4.2 it's as simple as:

使用 Swift 4.2 就这么简单:

extension Results {
  func toArray() -> [Element] {
    return compactMap {
      
extension Results {
    func toArray() -> [T] {
        return self.map { 
extension Results {
    func toArray<T>(type: T.Type) -> [T] {
        return flatMap { 
extension Results {
    func toArray<T>(type: T.Type) -> [T] {
        return compactMap { 
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
    }
}
as? T } } }
as? T } } }
} } }
} }

All the needed generics information is already a part of Resultswhich we extend.

所有需要的泛型信息已经是Results我们扩展的一部分。

回答by abdullahselek

This an another way of converting Resultsinto Array with an extension with Swift 3in a single line.

这是在一行中Results使用Swift 3扩展转换为 Array的另一种方式。

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
}

For Swift 4and Xcode 9.2

对于Swift 4和 Xcode 9.2

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
}

With Xcode 10flatMapis deprecated you can use compactMapfor mapping.

随着Xcode的10flatMap已被弃用,你可以使用compactMap映射。

func toArray<T>(ofType: T.Type) -> [T] {
    return flatMap { 
List(realm.objects(class))
as? T } }

回答by Jaseem Abbas

Swift 3

斯威夫特 3

var refrenceBook:[RefrenceProtocol] = []
let faceTypes = Array(realm.objects(FaceType))
refrenceBook = faceTypes.map({
extension Results {
    var array: [Element]? {
        return self.count > 0 ? self.map { 
Realm().objects(SomeClass.self).filter("someKey ENDSWITH %@", "sth").array
} : nil } }
as FaceType})

Usage

用法

extension Results {
    func toArray<T>(ofType: T.Type) -> [T] {
        let array = Array(self) as! [T]
        return array
    }
}

Alternative : Using generics

替代方法:使用泛型

let array = Realm().objects(SomeClass).toArray(ofType: SomeClass.self)

回答by Nosov Pavel

it's not a good idea to convert Results to Array, because Results is lazy. But if you need try this:

将结果转换为数组不是一个好主意,因为结果是惰性的。但如果你需要试试这个:

class func getSomeObject() -> [SomeObject]? {
    var someObjects: [SomeObject] = []
    let objects = Realm().objects(SomeObject)
    for object in objects{
        someObjects += [object]
    }
    return objects.count > 0 ? someObjects : nil
}

but better way is to pass Results wherever you need. Also you can convert Results to List instead of Array.

但更好的方法是在您需要的任何地方传递结果。您也可以将结果转换为列表而不是数组。

##代码##

if the first func is not working you can try this one:

如果第一个 func 不起作用,您可以尝试这个:

##代码##

回答by lindaaak

##代码##

So, you can use like:

所以,你可以使用像:

##代码##

回答by Vinayak

Solution for Swift 4, Realm 3

Swift 4, Realm 3 的解决方案

##代码##

Now converting can be done as below

现在转换可以如下完成

##代码##

回答by nRewik

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.

但是您可以通过创建一个 Swift 数组并将其附加到循环中来实现。

##代码##

If you feel it's too slow. I recommend you to pass around Realm Resultsobject directly.

如果你觉得它太慢了。我建议你Results直接传递 Realm对象。