简体   繁体   English

Swift Realm使用Array过滤List属性

[英]Swift Realm filter List property using an Array

I have this property in my Realm Object 我在我的Realm对象中有这个属性

 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]()

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 因此,如果我单击按钮A,它会将“A”附加到filteredList数组,如果我单击按钮B,它会将“B”附加到filteredList数组,依此类推

Currently this is my filter predicate 目前这是我的过滤谓词

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

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”(确切的ABCD)过滤标签,它将返回包含A,B,C的其他标签,或者D.

How can I get the tag with exact "A,B,C,D" in my search predicate? 如何在我的搜索谓词中获得具有精确“A,B,C,D”的标记?

Any help given is highly appreciated. 任何帮助都非常感谢。

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 你无法使用Realm的谓词来实现你的目标,因为Realm使用Predicates和缺少处理计算属性的能力有很多限制,但你可以用这种方式作为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())})

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 这里是Array($0.tags).map({$0.tagName}).sorted().joined()我们得到tags数组并将它转换为一个字符串数组然后我们对字符串数组进行排序(这将确保只关注数组中的TAGS而不是顺序)然后我们在String中转换那个排序数组,例如你的tags.tagName数组是[“B”,“A”,“C”]然后你会得到“ABC”为STRING

after that we check if that STRING contains your filterList.sorted().joined() the same procedure that was explained before 之后我们检查STRING是否包含你的filterList.sorted()。joined()与之前解释过的相同的过程

so if your filterList have ["B","C","A"] you will get "ABC" 所以,如果您的filterList有[“B”,“C”,“A”],您将获得“ABC”

and the we check if "ABC" contains "ABC" if so then is included in final result 并且我们检查“ABC”是否包含“ABC”,如果是,则包含在最终结果中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM