Filtering Realm objects with Swift

前提是你 提交于 2019-12-10 13:42:11

问题


I always get the following error when trying to filter my Realm database using NSPredicate:

Property 'text' is not a link in object of type 'getType'

I want to filter my Realm database to show only the items that have some specific text in them. This is what I've tried:

let realm = try! Realm()
let predicate = NSPredicate(format: "typez.text.filter = 'special'")
let filterThis = realm.objects(Publication).filter(predicate)
print(filterThis)

The relevant portion of my model classes is:

class Publication: Object, Mappable {
    dynamic var id: Int = 0
    var typez = List<getType>()
    dynamic var url: String?
}

class getType: Object, Mappable {
    dynamic var text: String = ""
}

回答1:


I don't usually use NSPredicate's directly, instead I do an inline predicate closure within the filter paramter.

let realm = try! Realm()
                     //Array of publications             
    let realmObjects = realm.objects(Publication)
    //any publication where .text property == special will be filtered. and filter out empty array
    let filterThis = realmObjects.filter({ $0.getType.filter({ $0.text == "special" } != [] ) })
    print(filterThis)



回答2:


You mentioned that the relevant portions of you model classes look like so:

class Publication: Object, Mappable {
    dynamic var id: Int = 0
    var typez = List<getType>()
    dynamic var url: String?
}

class getType: Object, Mappable {
    dynamic var text: String = ""
}

If I understand you correctly, you want to find Publication instances that have an entry in their typez list with text equal to special. You can express that as:

let realm = try! Realm()
let result = realm.objects(Publication).filter("ANY typez.text = 'special'")
print(result)



回答3:


I was not liking the accepted answer here because it doesn't actually answer the question... but then it helped me more than I realized. I will now be using closures instead of NSPredicates whenever possible. The actual answer to this question should be a slightly modified version of @NSGangster's answer:

let realm = try! Realm()
    //Array of publications             
    let realmObjects = realm.objects(Publication)
    //any publication where .text property == special will be filtered. and filter out empty array
    let filterThis = realmObjects.filter({ $0.typez.filter({ $0.text == "special" } != [] ) })
    print(filterThis)

.. or something close to that.

But what I was looking for was a bit different. I needed a way to filter on exact words of a multi-word string, and using an NSPredicate with "CONTAINS" would match any containing substring, e.g. a search for "red" would match "fred". Realm doesn't support "LIKE" or regex yet, so using a closure was the only thing I could get to work:

//I was going for a "related terms" result for a dictionary app
let theResults = terms.filter(
    {
        //Looking for other terms in my collection that contained the
        //title of the current term in their definition or more_info strings
        $0.definition.components(separatedBy: " ").contains(term.title) ||
        $0.more_info.components(separatedBy: " ").contains(term.title)
    }
)

With as much of the day as I spent searching, hopefully this helps someone else with a similar issue.



来源:https://stackoverflow.com/questions/36843480/filtering-realm-objects-with-swift

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