Sou*_*ock 6 realm nspredicate swift
尝试使用以下方法过滤我的Realm数据库时,我总是遇到以下错误NSPredicate:
属性'text'不是'getType'类型对象中的链接
我想过滤我的Realm数据库,只显示其中包含特定文本的项目.这就是我尝试过的:
let realm = try! Realm()
let predicate = NSPredicate(format: "typez.text.filter = 'special'")
let filterThis = realm.objects(Publication).filter(predicate)
print(filterThis)
Run Code Online (Sandbox Code Playgroud)
我的模型类的相关部分是:
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 = ""
}
Run Code Online (Sandbox Code Playgroud)
bda*_*ash 10
您提到模型类的相关部分如下所示:
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 = ""
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,您希望找到列表中具有等于Publication的条目的实例.你可以表达为:typeztextspecial
let realm = try! Realm()
let result = realm.objects(Publication).filter("ANY typez.text = 'special'")
print(result)
Run Code Online (Sandbox Code Playgroud)
NSG*_*ter -1
我通常不直接使用 NSPredicate,而是在过滤器参数中执行内联谓词闭包。
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14514 次 |
| 最近记录: |