信任大家看完后会和自身一样认为那些类真的很

2019-09-13 11:20栏目:大奖888官网登录
TAG:

NSMutableArray

使用:

==筛采取法

和NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID BETWEEN {1, 13}"];

NOT(非,可以用!代替)

@"name ENDSWITH[d] 'ang'"      //以有个别字符串结束

block中第二个参数代表其实便是急需筛选的数组对象的里边二个分子,第三个参数现今没搞理解功能

筛选出长度超越3的字符串

(2)范围运算符:IN、BETWEEN

表示三个字符NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE '?ng'"];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"code == %@ OR name == %@ ", @1, @"asb"];

打印

IN之中NSNumber类型,决断testID属性是不是是NSNumber对象中的@1和@13中的二个:NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID IN {1, 13}"]

判断code是否>=1且<=3

假使自己不想用任何实例方法,想筛选成员本人应当怎么办吧。那时候就足以用self来代替

AND(或&&)NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID >= %@ AND testID <=%@", @1ll, @13ll];

NSString *strRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,5}";

NSPredicate *pre = [NSPredicate predicateWithFormat:@"self CONTAINS %@", @3];

正如运算符符号使用

NSString *strRegex = @"[0-9]{1,20}";

(1)相比运算符>,<,==,>=,<=,!=

NSArray *array = @[@"jim", @"cook", @"jbos"]; NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { return [[evaluatedObject valueForKey:@"name"] isEqualToString:@"cook"]; }]; NSArray *aray = [array filteredArrayUsingPredicate:pre];

司空见惯用于判定NSNumber对象

NSPredicate *predicateA = [NSPredicate predicateWithFormat:@"SELF  in %@",arrayB];

NSArray *resultArray = [arrayA filteredArrayUsingPredicate:predicateA];

字符串相关CONTAINSNSArray *stringArray = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil]; NSString *string = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string]; NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred])

Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;NSArray *array = @[test1,test2,test3];NSPredicate *pre = [NSPredicate predicateWithFormat:@"name LIKE %@", @"?b*"];NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

例:@"name LIKE[cd] '*er*'"    //*表示通配符,Like也接受[cd].

自己性质NSArray *array = @[@"jim", @"cook", @"jobs", @"sdevm"]; NSPredicate *pre = [NSPredicate predicateWithFormat:@"length > 3"]; NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

?代表二个字符

NSArray *array = @[@"jim", @"cook", @"jobs", @"sdevm"];

上述打字与印刷都以一模二样的结果二〇一二-11-24 14:01:49.324 Nurse[5044:70b] ( shanghai, guangzou)

NSPredicate,这么些类和自己上一篇博文中关系的valueForKeyPath毫发不爽很强劲。它的使用重要集聚在八个办法中

(

上面小编就来挨家挨户介绍这几个类的用法,相信我们看完后会和自身同一以为那么些类真的很有力。

cook,

NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID > %@",@12ll];

通配符 LIKE

筛选出长度超过3的字符串

为此只有 angle符合

(

例:@“SELF == ‘APPLE’"

有同等的功能NOT从贰个数组中过滤掉别的二个数组的持有数据NSArray *arrayFilter = @[@"abc1", @"abc2"]; NSArray *arrayContent = @[@"a1", @"abc1", @"abc4", @"abc2"]; NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter]; NSLog(@"%@",[arrayContent filteredArrayUsingPredicate:thePredicate]);

NSString *string=@"assdbfe";NSString *targetString=@"^a.+e$";NSPredicate *pres = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", targetString];BOOL match = [pres evaluateWithObject:string];

@"name LIKE[cd] '???er*'"

限定运算符符号解释使用

lenght正是对数组成员实行[xxxx lenght]然后判别再次来到的NSUInteger值是还是不是高于3。扩张到NSString别的艺术比方integerValue

应用成员实例方法

协作用法

在好几景况下,类似于地点例子中的code字符串不是很肯定,创设的时候就能那样使用

jobs,

SELF CONTAINS表示作者满含[c] 忽略大小写[d] 忽略重音符号[cd]既不区分轻重缓急写,也不区分发音符号。

还也可以有NSSet和NSMutableSet也足以用这几个类筛选。

NSPredicate *pre = [NSPredicate predicateWithFormat:@"integerValue >= %@", @3];

无差异于的作用ORNSPredicate *pred = [NSPredicate predicateWithFormat:@"title == 'angle' OR title == 'lenang'"];

最后

1、数组交集

意味着四个要么八个可能是空NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE 'ang*'"];

NOT最布满的用法就是从三个数组中除去别的四个数组的数据,也可以有一点绕,举个例子就很爽朗了。

(4)字符串相关:BEGINSWITH、ENDSWITH、CONTAINS

和NSPredicate *pred = [NSPredicate predicateWithFormat:@"title IN {'angle', 'addss'}"];

*代表四个依旧八个也许是空

(5)通配符:LIKE

IN之中NSString类型,决断title属性是还是不是是字符串@"angle"和@"addss"中的三个:NSPredicate *pred = [NSPredicate predicateWithFormat:@"title IN {'angle', 'addss'}"];

###NSString对象的操作

例:@"number BETWEEN {1,5}"

title CONTAINStitle即是Test对象中的贰天性子BEGINSWITH(已有个别字符串开端, begins with)NSString *targetString = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title BEGINSWITH %@",targetString];

NSPredicate *pre = [NSPredicate predicateWithFormat:@"%K == %@", @"code", @2];

)

点名字段过滤NSPredicate *pred = [NSPredicate predicateWithFormat:@" NOT (testID IN %@ )",@[@1, @2]];

sdevm

可用于数值及字符串

!=NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID != %@",@1];

打印

lenght就是对数组成员实行[xxxx lenght]接下来剖断再次来到的NSUInteger值是不是高于3。增加到NSString其余方法举个例子integerValue

==NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID == %@",@12ll];

是否以a开头以e结尾

NSArray *array = @[@"2", @"3", @"4", @"5"];

BETWEEN之间{1, 13}包括1和13:NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID BETWEEN {1, 13}"]

OR(或,可以用||代替)

sdevm

?

)

注:[c]不区分轻重缓急写[d]不区分发音符号即未有重音符号[cd]既不区分轻重缓急写,也不区分发音符号。

  • evaluateWithObject:object;

实在==不只好够用来相比较NSNumber对象,还足以用来判别NSString对象是或不是同样。

NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

所以angle和lenang都符合

筛选拔法

NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

<NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID < %@",@1];

假诺急需格外数性情子的筛选,用AND恐怕OEnclave来串联显明不怎么艰苦,NSCompoundPredicate类能够知足我们的须求,它能够将多少个NSPredicate对象的构成,组合格局得以是AND恐怕OSportage。

@"name BEGINSWITH[c] 'sh'"     //以有些字符串起头

  • (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate;

在意:%K的K必需是大写。

3.主导语法

CONTAINS也能够用来自定义对象中NSArray *stringArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil]; NSString *targetString = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS %@",targetString]; NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred]);

Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;NSArray *array = @[test1,test2,test3];NSPredicate *pre = [NSPredicate predicateWithFormat:@"%@ == %@", @"code", @2];NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

@"address IN {'shanghai','beijing'}"

Block

是还是不是是手提式有线电话机号

(3)字符串本人:SELF

筛选用法NSArray

OOdyssey可以用来顶替IN达到一样的机能,不过O本田UR-V越来越灵敏。

NSPredicate *pre = [NSPredicate predicateWithFormat:@"length > 3"];

  • filterUsingPredicate:(NSPredicate *)predicate;

只能说,在利用+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;方法成立的NSPredicate对象的时候有个坑。

例:@"name CONTAIN[cd] 'ang'"   //满含有个别字符串

[cd]的用法NSArray *stringArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil]; NSString *targetString = @"Ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@",targetString]; NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred]);

NSArray

通配符 LIKE符号解释使用备注

实际上NSPredicate不只可以够用来筛选,还足以用来判断相称直接重临是不是适合,首要方法是- (BOOL)evaluateWithObject:(id)object;,用法:

ENDSWITH(已某些字符串结尾, ends with)NSString *targetString = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title ENDSWITH %@",targetString];

NSArray *arrayFilter = @[@"abc1",@"abc2"];NSArray *arrayContent = @[@"a1",@"abc1",@"abc4",@"abc2"];    NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter];NSLog(@"%@",[arrayContent filteredArrayUsingPredicate:thePredicate]);

  • (NSPredicate*)predicateWithBlock:(id evaluatedObject, NSDictionary *bindings))block

NSPredicate差异常少能够满意全部方式的询问,合作Core Data的数据库查询当然不言自明。NSPredicate用法不只那些,有意思味的同校能够看下nshipster网址的那篇博文,里面涉及了一部分自己尚未涉嫌到的用法。

NSPredicate *pre = [NSPredicate predicateWithFormat:@"name == %@", @"西湖"];

ENDSWITH(已某些字符串结尾, ends with)

相称用法

万一小编不想用任何实例方法,想筛选成员本身应当咋做吗。那时候就足以用self来代替

BETWEEN(之间)

NSPredicate *pre = [NSPredicatepredicateWithFormat:@"code == %@ OR code == %@ ",@1,@3];

NSPredicate *pre1 = [NSPredicatepredicateWithFormat:@"code >= %@",@3];NSPredicate *pre2 = [NSPredicatepredicateWithFormat:@"code <= %@",@2];//以AND情势整合NSPredicate *pre = [NSCompoundPredicateandPredicateWithSubpredicates:@[pre1,pre2]];//以O传祺格局构成NSPredicate *pre = [NSCompoundPredicateorPredicateWithSubpredicates:@[pre1, pre2]];

就不会被坑

@interfaceTest :NSObject@property (nonatomic,strong)NSString *name;@property (nonatomic,strong)NSNumber *code;@end

自然最常用的要么同盟卓殊正则表达式,列举多少个常用的正则

- (void)filterUsingPredicate:(NSPredicate *)predicate;

}];

AND(且,可以用&&代替)

意义和IN同样,可是O本田UR-V能够判定不只一个属性

当决断的时候须要忽略大小写能够行使[cd]

打印

BEGINSWITH(已某些字符串起初, begins with)

NSString *targetString = @"ing";NSPredicate *pre = [NSPredicate predicateWithFormat:@"name ENDSWITH %@",targetString];

应用成员实例方法

NSPredicate *pre = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", @"abc"];

NSString *targetString = @"h";NSPredicate *pre = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@",targetString];

Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;    NSPredicate *pres = [NSPredicate predicateWithFormat:@"code == %@", @2];    BOOL match = [pres evaluateWithObject:test1];

NSPredicate *pre = [NSPredicate predicateWithFormat:@"self CONTAINS %@", @3];

(    cook,jobs,

再推而广之到模型

比起循环相比较再加到新数组中,轻便的不停一两点。

NSPredicate *pre = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", @"?b*"];

[c] 忽略大小写

再涉及到一些更目迷五色的询问语句,举个例子剖断字符串以某些字符串开首或许结尾,通配符的运用。

筛选出数组成员[test code]格局(code属性的get方法)再次回到值>=2的成员。这里的可比运算符同样也得以选拔==、!=、<=、<。

Test *test1 = [[Test alloc]init];test1.name = @"西湖";test1.code = @1;    Test *test2 = [[Test alloc]init];test2.name = @"西溪湿地";test2.code = @2;    Test *test3 = [[Test alloc]init];test3.name = @"灵隐寺";test3.code = @3;    NSArray *array = @[test1,test2,test3];    NSPredicate *pre = [NSPredicate predicateWithFormat:@"code >= %@", @2];    NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

假定查询的是性质,举例code是Test类的天性,那么利用上边包车型大巴创立情势

结果是独有test1符合,like也足以承受[cd]

细心NSPredicate对象的开头化格局。运营那块代码你会发掘test2对象未有被询问出来。打字与印刷pre开掘"code" == 2,那表达查找的是"code"方法的再次回到值,那眼看行不通。

论及运算,富含了IN、BETWEEN、AND、OLX570、NOT

CONTAINS用法前面会讲到

Test.h

剖断code是不是@1要么是@2,也等于是不是在数组中。

参数evaluatedObject表示数组成员,block必得回到YES大概NO,分别代表相当照旧不合营。请忽略bindings参数,具体功效作者也没搞理解。

NSPredicate *pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"湖"];

NSPredicate *pre = [NSPredicatepredicateWithFormat:@"code IN %@", @[@1,@3]];

筛选出name是"千岛湖"的对象数组。

多种筛选

}

IN(之中)

abc4

前方提到的都以用+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;方法创制,还大概有另一种常用的主意:+ (NSPredicate*)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block,用Block格局成立

- (NSArray*)filteredArrayUsingPredicate:(NSPredicate *)predicate;

是或不是是邮箱

a1,

NSPredicate *pred = [NSPredicate predicateWithFormat:@"code BETWEEN {1, 3}"];

[d] 忽略重音符号

NSArray *array = @[@"2",@"3",@"4",@"5"];NSPredicate *pre = [NSPredicate predicateWithFormat:@"integerValue >= %@", @3];NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

前方提到==比较运算符能够起到- (BOOL)isEqualToString:(NSString *)aString;方法的功能,来判定字符串是或不是一律。那么字符串中蕴藏有些字符串应该怎么决断呢,在NSPredicate中能够用CONTAINS(大小写都足以)来代表包括关系。

NSPredicate *pred = [NSPredicatepredicateWithFormat:@"code >= %@ AND code <=%@",@1,@3];

NSMutableArray

)

[cd]既不区分轻重缓急写,也不区分发音符号。

NSArray *array = @[@"jim",@"cook",@"jobs",@"sdevm"];    NSPredicate *pre = [NSPredicate predicateWithFormat:@"length > 3"];NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject,NSDictionary *bindings) {    Test *test = (Test *)evaluatedObject;if (test.code.integerValue >2) {returnYES;    }else{returnNO;

版权声明:本文由大奖888-www.88pt88.com-大奖888官网登录发布于大奖888官网登录,转载请注明出处:信任大家看完后会和自身一样认为那些类真的很