I am writing a (Mac) application in Swift2 that is supposed to search for a Realm database from Teachers with a separate subject. The definition for the Teacher object in the field is as follows:
class Teacher: Object {
var subjects = List<Subject>()
}
This class is very complex, so I deleted a few lines ...
Here, the function should filter the database for teachers with specific topics and return only the names of teachers (as String Array:) [String]
:
func getAllTeacherNamesForSubject(subject: String) -> [String] {
let realm = openRealmDatabase()
let objects = realm.objects(Teacher).filter("!!! Need Help !!!", subject)
let objectsArray = objects.toArray(Teacher) as [Teacher]
return ???
}
extension Results {
func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for var i = 0; i < count; i++ {
if let result = self[i] as? T {
array.append(result)
}
}
return array
}
}
So the problem is that I do not know how to filter the database.
Can anybody help me?
source
share