Swift Realm使用Array过滤List属性 [英] Swift Realm filter List property using an Array

查看:649
本文介绍了Swift Realm使用Array过滤List属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的领域对象中拥有此属性

I have this property in my Realm Object

 var tags = List<Tag>()
 "tags": [
        {
            "tagId": "80069",
            "tagName": "A"
        },
        {
            "tagId": "80070",
            "tagName": "B"
        },
        {
            "tagId": "80071",
            "tagName": "C"
        },
        {
            "tagId": "80073",
            "tagName": "D"
        }
    ]

我有一个可以过滤掉标签的视图控制器。

I have a view controller that can filter out the tag.

所以我有几个按钮来切换过滤器。我所做的是为每个按钮创建一个过滤器数组

So i have several buttons to toggle the filter. What I have done is i create an array for the filter for each of my button

var filteredList = [String]()

所以,如果我点击按钮A,它会将A附加到filteredList数组,如果我单击按钮B,它会将B附加到filteredList数组,依此类推

So, if i click Button A, it will append "A" to the filteredList array, and if I click Button B, it will append "B" to the filteredList array and so on

目前这是我的过滤谓词

let realmFilteredList = self.realm.objects(MyDTO.self).filter("ANY tags.tagName IN %@", self.filteredList)

然而,上面的谓词给了我错误的结果,因为如果让我说我想用属性A,B,C,D过滤标签(确切的ABCD),它会返回包含A,B,C或D的其他标签。

However, above predicate gives me wrong result, because if let's say i want to filter the tag with property "A,B,C,D" (exact ABCD), it will return me other tag that contain either A,B,C,or D.

如何获得具有完全A的标签B,C,D在我的搜索谓词中?

How can I get the tag with exact "A,B,C,D" in my search predicate?

我们非常感谢任何帮助。

Any help given is highly appreciated.

推荐答案

使用Realm的谓词无法实现目标,因为Realm使用有很多限制谓词以及缺少处理计算属性的能力,但你可以使用这种方式作为一个workarround

You can't achieve your goals using predicate with Realm because Realm have a lot of limitations using Predicates and the missing ability to handle computed properties but you can use this way as a workarround

    let filterList = ["A","B"]
    let realmList = realmInstance?.objects(MyDTO.self)
    let filteredArray = Array(realmList!).filter({Array($0.tags).map({$0.tagName}).sorted().joined().contains(filterList.sorted().joined())})

此处数组($ 0.tags).map({$ 0.tagName})。sorted()。joined()我们得到了标记数组并将其与map转换为字符串数组然后我们对字符串数组进行排序(这将确保只关注数组中的TAGS而不是顺序)然后我们通过示例将数组中的排序数组转换为数组tags.tagName是[B,A,C]之后你会得到ABC为STRING

here Array($0.tags).map({$0.tagName}).sorted().joined() we get the tags array and convert it with map to an array of Strings then we sort that array of Strings (this will ensure that only matters the TAGS in the array not the order) and after that we convert that sorted array in a String by example your array of tags.tagName is ["B","A","C"] and after this you will get "ABC" as STRING

之后我们检查STRING是否包含你的filterList.sorted()。joined()与之前解释的相同的过程

after that we check if that STRING contains your filterList.sorted().joined() the same procedure that was explained before

所以如果你的filterList有[B, C,A]你将获得ABC

so if your filterList have ["B","C","A"] you will get "ABC"

并且我们检查ABC是否包含ABC,如果是,则包括在最终结果中

and the we check if "ABC" contains "ABC" if so then is included in final result

这篇关于Swift Realm使用Array过滤List属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆