A type-safe Realm query extensions using Swift 4 Smart KeyPaths
A type-safe Realm query extensions using Swift 4’s Smart KeyPaths.
RealmTypeSafeQuery is written in Swift 4.
RealmTypeSafeQuery is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'RealmTypeSafeQuery'
For Carthage, add the following to your Cartfile
:
github "kishikawakatsumi/RealmTypeSafeQuery"
RealmTypeSafeQuery provides type safety, code completion and avoidance of typos against NSPredicate
queries.
realm.objects(Person.self).filter("name == %@", "Katsumi")
realm.objects(Person.self).filter("age > %@", 20)
realm.objects(Person.self).filter(\Person.name == "Katsumi")
realm.objects(Person.self).filter(\Person.age > 20)
The following code should be a compile error.
realm.objects(Person.self).filter(\Person.name > 20) // Compile error
realm.objects(Person.self).filter(\Dog.name == "John") // Compile error
It requires a string representation of KeyPath
to construct NSPredicate
from KeyPath
. However, the API is not officially visible currently. The feature request is tracked at SR-5220.
[SR-5220] Expose API to retrieve string representation of KeyPath - Swift