目錄:
-NSPredicate
-NSSortDescriptor
-KVC
NSPredicate(謂詞)
NSPredicate這個類很多人都很陌生。
這是蘋果提供的快速篩選對象數(shù)據(jù)的一個過濾器由境,既強大又簡潔蓖议。
在CoreData會體現(xiàn)很明顯讥蟆。
關鍵字
ANDOR這些不解釋
=><>=<===!=:基本運算符
BETWEEN {x,y}:范圍運算,不能使用字符串類型
IN{x,.....}:包含 字符串與數(shù)字都可用
MATCHES:正則表達式
字符串匹配:
1.LIKE:模糊查詢
2.CONTAINS:包含在字符串
3.BEGINSWITH:字符串開頭
4.ENDSWITH:字符串結束
5.SELF:字符串本身瘸彤,在字符串數(shù)組用到。
語法格式
對象字段 匹配關鍵字 匹配字段 如:age == 123
多個匹配 可以加 AND OR? 基本跟sql一樣 如:name == 'xxx' AND age == 123
//偽代碼 Food {name,monry}
@property(copy,nonatomic)NSArray*sources;
例如:我要篩選對象(Food)數(shù)組中變量名為name的质况,而它值為呵呵的對象出來。
以前的做法:
NSMutableArray*foods = [@[] mutableCopy];
for(Food *foodinself.sources) {
if([food.name isEqualToString:@"呵呵"]) {? ? ? ?
[arrayM addObject:food];?
? }}
現(xiàn)在的做法:
NSPredicate*predicate = [NSPredicatepredicateWithFormat:@"name == %@",@"呵呵"];
NSArray*foods = [self.sources filteredArrayUsingPredicate:predicate];
就會發(fā)現(xiàn)突然高大上了中贝。Y(_)Y
當然這些還是比較基礎的。臼朗。竟纳。
比較常用的:
//查詢字符串匹配? [cd]:不區(qū)分大小寫轻局,只要是字符串匹配的關鍵字都可以用[cd] // *:等同于在sql的%NSString* searchText =@"xxx";//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@",[NSString stringWithFormat:@"*%@*",searchText]];
NSPredicate*predicate = [NSPredicatepredicateWithFormat:@"name CONTAINS[cd] %@",searchText];//等同上面寫法
NSArray*searchFoods = [self.source filteredArrayUsingPredicate:predicate];
//BETWEEN{x,y} NSPredicate*between = [NSPredicatepredicateWithFormat:@"monry BETWEEN{10,100}"];
NSArray*betweenFoods = [self.sources filteredArrayUsingPredicate: between]; //IN{x,.....}
NSPredicate*inPred = [NSPredicatepredicateWithFormat:@"name IN{%@,%@}",@"xx",@"xxxx"];
NSArray* inPredFoods = [self.sources filteredArrayUsingPredicate: inPred];//MATCHES 正則
NSString*regex =@"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";//手機號NSPredicate*regexTestMobile = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];
BOOLisPhone = [regexTestMobile evaluateWithObject:@"13111111111"];
NSSortDescriptor(排序)
排序方面就不寫老做法了挡毅。(就是懶*\(o)/*)
NSSortDescriptor*sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"monry"ascending:YES];
NSArray*foods = [self.sources sortedArrayUsingDescriptors:@[sortDescriptor]];
//這也是比較常用的用法,這里做簡單的判斷跪呈,等同于上面寫法
[self.sources sortUsingComparator:^NSComparisonResult(Food * _Nonnull obj1, Food *_Nonnull obj2) {
? ? return !sender.selectedSegmentIndex?[obj1.monry compare:obj2.monry]:[obj2.monry compare:obj1.monry];
}];
*/
當然這個只是簡單排序。
復雜一點需要自己寫邏輯也可以使用NSArray或NSSet里面提供方法的庆械。
@interfaceNSArray (NSExtendedArray)
...- (NSArray *)sortedArrayUsingFunction:(NSInteger(*)(ObjectType, ObjectType,void* __nullable))comparator context:(nullablevoid*)context;
- (NSArray *)sortedArrayUsingFunction:(NSInteger(*)(ObjectType, ObjectType,void* __nullable))comparator context:(nullablevoid*)context hint:(nullableNSData*)hint;
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;...- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
@end
@interface NSMutableArray (NSExtendedMutableArray)
...- (void)sortUsingFunction:(NSInteger(*)(ObjectType,? ObjectType,void* __nullable))compare context:(nullablevoid*)context;
- (void)sortUsingSelector:(SEL)comparator;
...- (void)sortUsingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
@end
還有些這里就不占字數(shù)了缭乘,自己找去( ′ ▽ ` )?。
使用 KVC 計算
關鍵字
@min:最小值
@max:最大值
@avg:平均值
@sum:總和
基礎運算
NSIntegermin = [[self.sources valueForKeyPath:@"@min.monry"] integerValue];
NSIntegermax = [[self.sources valueForKeyPath:@"@max.monry"] integerValue] ;
NSIntegersum =[[self.sources valueForKeyPath:@"@sum.monry"] integerValue];
double avg = [[self.sources valueForKeyPath:@"@avg.monry"] doubleValue] ;
使用后瞬間覺得世界美好了很多堕绩。
總結:
當然這些都是看項目里面邏輯需求邑时,如何搭配看個人了(☆_☆)。
最簡單的實戰(zhàn)經(jīng)驗:我想計算我本地未讀消息的總和晶丘,然后顯示到Tabbar Badge 或者顯示到App Icon Badge 里面去唐含。
邏輯:使用NSPredicate篩選出未讀消息會話,然后@sum.count捷枯,最后賦值。
當然也要看需求淮捆,還有你實現(xiàn)邏輯,來取決你的計算攀痊。