Clifton Labrum Clifton Labrum - 1 month ago 13
Swift Question

Search Realm Relationship in Swift 3

Swift 3, Realm 2.1

I'm trying to figure out how to search my

Aircraft
objects based on values in its related
fieldValues
list. Here is a simplified view of my class structure:

class Aircraft: Object {
dynamic var makeModel = ""
let fieldValues = List<FieldValue>()
}

class FieldValue: Object{
dynamic var name = ""
}


I can search the
makeModel
value (where
search!
is my search term) like this:

let makeModelPredicate = NSPredicate(format: "makeModel BEGINSWITH[c] %@", search!)


...but how do I check to see if
search
matches any
name
values in the
fieldValues
list?

Answer

You can query over key paths: NSPredicate(format: "ANY fieldValues.name = %@", search!)