具有不同 table 视图部分的领域过滤器字符串
Realm filter strings with different table view sections
我有一个 table 视图,最多包含 27 个部分 (A-Z + #)
A 部分包含名称以 "A" 等开头的所有对象。在这种情况下,我的对象是 Artist
类型并具有 name
属性。
private var sectionIndices: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"]
for beginningLetter in self.sectionIndices {
print("Finding artists for \(beginningLetter)")
let artists: Results<Artist>
if beginningLetter == "#" {
artists = self.artists.filter("NOT (name[0] IN %@)", self.sectionIndices)
} else {
artists = self.artists.filter("name BEGINSWITH[cd] %@", beginningLetter).sorted(byKeyPath: "name", ascending: true)
}
}
因此过滤对除 # 部分以外的所有内容都非常有效。基本上每个名字以非字母开头的艺术家都应该在那里。 “21 Savage”、“6ix9ine”、“2 Pac”等
由于某种原因 MATCHES 不起作用,我试过了
name MATCHES %@
但 realm 并不真正支持。
我正在寻找一个能给我这种行为的过滤器 - name[0]
上的 IN 运算符也不起作用(而且效率低下)。
感谢任何帮助,谢谢!
可悲的是,Realm 确实不支持 NSPredicate
的 MATCHES
运算符。针对您的特定问题的一种可能的解决方法是使用 BEGINSWITH
运算符和所有可能的单个数字,并使用 OR
s 和所有数字制作复合谓词。
let startsWithNumberPredicate = NSPredicate(format: "name BEGINSWITH '0' OR name BEGINSWITH '1' OR name BEGINSWITH '2' OR name BEGINSWITH '3' OR name BEGINSWITH '4' OR name BEGINSWITH '5' OR name BEGINSWITH '6' OR name BEGINSWITH '7' OR name BEGINSWITH '8' OR name BEGINSWITH '9'")
然后你只需要使用这个 startsWithNumberPredicate
in case beginningLetter == "#"
来找到所有 Artist
的 name
属性 以数字开头。
private var sectionIndices: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"]
for beginningLetter in self.sectionIndices {
print("Finding artists for \(beginningLetter)")
let artists: Results<Artist>
if beginningLetter == "#" {
artists = self.artists.filter(startsWithNumberPredicate, self.sectionIndices)
} else {
artists = self.artists.filter("name BEGINSWITH[cd] %@", beginningLetter).sorted(byKeyPath: "name", ascending: true)
}
}
我有一个 table 视图,最多包含 27 个部分 (A-Z + #)
A 部分包含名称以 "A" 等开头的所有对象。在这种情况下,我的对象是 Artist
类型并具有 name
属性。
private var sectionIndices: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"]
for beginningLetter in self.sectionIndices {
print("Finding artists for \(beginningLetter)")
let artists: Results<Artist>
if beginningLetter == "#" {
artists = self.artists.filter("NOT (name[0] IN %@)", self.sectionIndices)
} else {
artists = self.artists.filter("name BEGINSWITH[cd] %@", beginningLetter).sorted(byKeyPath: "name", ascending: true)
}
}
因此过滤对除 # 部分以外的所有内容都非常有效。基本上每个名字以非字母开头的艺术家都应该在那里。 “21 Savage”、“6ix9ine”、“2 Pac”等
由于某种原因 MATCHES 不起作用,我试过了
name MATCHES %@
但 realm 并不真正支持。
我正在寻找一个能给我这种行为的过滤器 - name[0]
上的 IN 运算符也不起作用(而且效率低下)。
感谢任何帮助,谢谢!
可悲的是,Realm 确实不支持 NSPredicate
的 MATCHES
运算符。针对您的特定问题的一种可能的解决方法是使用 BEGINSWITH
运算符和所有可能的单个数字,并使用 OR
s 和所有数字制作复合谓词。
let startsWithNumberPredicate = NSPredicate(format: "name BEGINSWITH '0' OR name BEGINSWITH '1' OR name BEGINSWITH '2' OR name BEGINSWITH '3' OR name BEGINSWITH '4' OR name BEGINSWITH '5' OR name BEGINSWITH '6' OR name BEGINSWITH '7' OR name BEGINSWITH '8' OR name BEGINSWITH '9'")
然后你只需要使用这个 startsWithNumberPredicate
in case beginningLetter == "#"
来找到所有 Artist
的 name
属性 以数字开头。
private var sectionIndices: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"]
for beginningLetter in self.sectionIndices {
print("Finding artists for \(beginningLetter)")
let artists: Results<Artist>
if beginningLetter == "#" {
artists = self.artists.filter(startsWithNumberPredicate, self.sectionIndices)
} else {
artists = self.artists.filter("name BEGINSWITH[cd] %@", beginningLetter).sorted(byKeyPath: "name", ascending: true)
}
}