SwiftUI-CoreData中使用NSPredicate过滤
在利用CoreData来存储数据的时候,如果需要过滤出我们需要的结果怎么办呢?
NSPredicate
1 | NSPredicate : NSObject |
where to use
1 | (entity: , sortDescriptors: , predicate: , animation: ) |
Usage
init
1 | NSPredicate(format: "universe == 'Star'")) |
%@
是占位符
Comparison
1 | grade == "7" |
==
和like
关键字都可以,此外还有<
或>
Involve
1 | "universe IN %@", ["Aliens", "Firefly", "Star Trek"] |
(In the example, like[cd]
is a modified “like” operator that is case-insensitive and diacritic-insensitive.)
more to see Predicate Format String Syntax
Regex
1 | "name matches %@", "[0-9]o{2}" |
Logic
1 | "name BEGINSWITH %@", "E" |
还可以使用NOT
AND
OR
等基础关键字
Advanced
To see Predicate Programming Guide
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 X Mεl0n | 随手记!