使用 Swift 过滤 Realm 对象
Filtering Realm objects with Swift
尝试使用 NSPredicate
过滤我的 Realm 数据库时,我总是遇到以下错误:
Property 'text' is not a link in object of type 'getType'
我想过滤我的 Realm 数据库以仅显示其中包含特定文本的项目。这是我试过的:
let realm = try! Realm()
let predicate = NSPredicate(format: "typez.text.filter = 'special'")
let filterThis = realm.objects(Publication).filter(predicate)
print(filterThis)
我的模型 类 的相关部分是:
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 = ""
}
我通常不直接使用 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({ [=10=].getType.filter({ [=10=].text == "special" } != [] ) })
print(filterThis)
您提到您模型 类 的相关部分如下所示:
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 = ""
}
如果我没理解错的话,您想要找到 Publication
个实例,这些实例在其 typez
列表中有一个 text
等于 special
的条目。您可以将其表示为:
let realm = try! Realm()
let result = realm.objects(Publication).filter("ANY typez.text = 'special'")
print(result)
我不喜欢这里接受的答案,因为它实际上并没有回答问题......但它对我的帮助比我意识到的要多。我现在将尽可能使用闭包而不是 NSPredicates。这个问题的实际答案应该是@NSGangster 答案的略微修改版本:
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({ [=10=].typez.filter({ [=10=].text == "special" } != [] ) })
print(filterThis)
.. 或类似的东西。
但我要找的有点不同。我需要一种方法来过滤多词字符串的确切词,并且使用带 "CONTAINS" 的 NSPredicate 将匹配任何包含的子字符串,例如搜索 "red" 将匹配 "fred"。 Realm 还不支持 "LIKE" 或正则表达式,所以使用闭包是我唯一可以开始工作的东西:
//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
[=11=].definition.components(separatedBy: " ").contains(term.title) ||
[=11=].more_info.components(separatedBy: " ").contains(term.title)
}
)
我花了一天的时间进行搜索,希望这对遇到类似问题的其他人有所帮助。
尝试使用 NSPredicate
过滤我的 Realm 数据库时,我总是遇到以下错误:
Property 'text' is not a link in object of type 'getType'
我想过滤我的 Realm 数据库以仅显示其中包含特定文本的项目。这是我试过的:
let realm = try! Realm()
let predicate = NSPredicate(format: "typez.text.filter = 'special'")
let filterThis = realm.objects(Publication).filter(predicate)
print(filterThis)
我的模型 类 的相关部分是:
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 = ""
}
我通常不直接使用 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({ [=10=].getType.filter({ [=10=].text == "special" } != [] ) })
print(filterThis)
您提到您模型 类 的相关部分如下所示:
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 = ""
}
如果我没理解错的话,您想要找到 Publication
个实例,这些实例在其 typez
列表中有一个 text
等于 special
的条目。您可以将其表示为:
let realm = try! Realm()
let result = realm.objects(Publication).filter("ANY typez.text = 'special'")
print(result)
我不喜欢这里接受的答案,因为它实际上并没有回答问题......但它对我的帮助比我意识到的要多。我现在将尽可能使用闭包而不是 NSPredicates。这个问题的实际答案应该是@NSGangster 答案的略微修改版本:
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({ [=10=].typez.filter({ [=10=].text == "special" } != [] ) })
print(filterThis)
.. 或类似的东西。
但我要找的有点不同。我需要一种方法来过滤多词字符串的确切词,并且使用带 "CONTAINS" 的 NSPredicate 将匹配任何包含的子字符串,例如搜索 "red" 将匹配 "fred"。 Realm 还不支持 "LIKE" 或正则表达式,所以使用闭包是我唯一可以开始工作的东西:
//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
[=11=].definition.components(separatedBy: " ").contains(term.title) ||
[=11=].more_info.components(separatedBy: " ").contains(term.title)
}
)
我花了一天的时间进行搜索,希望这对遇到类似问题的其他人有所帮助。