NSPredicate提供的谓词可以分为两类:比较谓词和复合谓词。 比较谓词:比较谓词通过使用比较运算符来描述所符合条件的属性状态。 二、NSPredicate类的应用解析 NSPredicate提供创建谓词对象和解析谓词对象的方法,它也是Cocoa中有关谓词的类中的基类。 NSPredicate提供了如下函数来进行初始化: //通过格式化字符串来进行谓词对象的初始化 + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat 有一个小细节需要注意,在进行格式化时,如果使用的是变量则不需要添加引号,解析器会帮助你添加,如果使用到常量,则要用转义字符进行转义,例如: NSPredicate * predicate = [NSPredicate *)predicateWithValue:(BOOL)value; //自定义实现检索函数 /* 例如前面的示例也可以这样写 NSPredicate * predicate = [NSPredicate
NSString *attributeName = @"firstName"; NSString *attributeValue = @"siting"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", attributeName, attributeValue]; 1.2 Basic In Objective-C, you could create a BETWEEN predicate as shown in the following example: NSPredicate * [a-zA-Z]+$)[a-zA-Z0-9]{6,16}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^(([1-9]{1}[0-9]
[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex 手机号以13, 15,18开头,八个 \d 数字字符 NSString phoneRegex = @"^((13[0-9])|(15[^4,\D])|(18[0,0-9]))\d{8}$"; NSPredicate NSString carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z0-9\u4e00-\u9fa5]$"; NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex]; NSLog(@"carTest is %@",carTest *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; BOOL B =
一、NSPredicate基本语句 只要我们使用谓词(NSPredicate)都需要为谓词定义谓词表达式,而这个表达式必须是一个返回BOOL的值。 谓词表达式由表达式、运算符和值构成。 1.比较运算符 比较运算符如下 =、==:判断两个表达式是否相等,在谓词中=和==是相同的意思都是判断,而不是赋值 NSNumber *testNumber = @123; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF = 123"]; if ([predicate evaluateWithObject:testNumber NSArray *testArray = @[@1, @2, @3, @4, @5, @6]; NSPredicate *predicate = [NSPredicate predicateWithFormat *predTemp = [NSPredicate predicateWithFormat:@"%K > $VALUE", @"age"]; // 指定$VALUE的值为 25 NSPredicate
.m文件 /* 验证相关 */ //1.是否是手机号 - (BOOL)isMobileNumber{ NSString * MOBILE = @"^1[3-9]\\d{9}$"; NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE]; if ([regextestmobile +[a-zA-Z]{2,}$"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex [a-zA-Z]+$)[0-9A-Za-z]{8,14}$"; NSPredicate *pwdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES
+ (BOOL)checkEmployeeNumber: (NSString *) number { NSString *pattern = @"^[0-9]{11}"; NSPredicate * predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[0][0-9]+$"]; //匹配两位小数、整数 NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^(([1-9]{1}[0-9] YES : NO; } 2.2 支付密码/短信验证码 + (BOOL)isPWDChangeCharactersInRange:(NSString*)str{ NSPredicate * predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[0-9]{0,6}$"]; return [predicate0
下面介绍一个更简便的方法来实现 简介 NSPredicate类主要用来指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。 一般的, NSPredicate的筛选过滤的条件可以是, 逻辑运算符号(> , < , =),范围运算符(IN,BETWEEN),字符的包含/匹配/模糊搜索(BEGINSWITH,ENDSWITH,CONTAINS NSPredicate用法 1.创建NSPredicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@"过滤条件"]; *inputPredicate=[NSPredicate predicateWithFormat:condition,value]; NSMutableArray *newMutableArray *inputPredicate=[NSPredicate predicateWithFormat:@"%K like[cd] %@",key, [NSString stringWithFormat:@
前文中的name组 实例分析 匹配用户手机号 根据上面的语法,我用OC语言写出来的正则表达式匹配手机号的代码如下: NSString *pattern = @"^1+[3578]+\\d{9}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject [a-zA-Z]+$)[a-zA-Z0-9]{6,18}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES checkUserName : (NSString *) userName { NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}"; NSPredicate + (BOOL)checkEmployeeNumber : (NSString *) number { NSString *pattern = @"^[0-9]{12}"; NSPredicate
文章脉络 谓词(NSPredicate) Predicate(谓语)的意思。NSPredicate类是用来定义逻辑条件约束的获取或内存中的过滤搜索。 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF = 123"]; if ([predicate evaluateWithObject NSArray *testArray = @[@1, @2, @3, @4, @5, @6]; NSPredicate *predicate = [NSPredicate predicateWithFormat = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM]; NSPredicate *regextestcu = [NSPredicate \\w+)*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strRegex];
一、谓词的基本概念与使用 1、谓词(NSPredicate)用于定义一个逻辑条件,通过该条件可执行搜索或内存中的过滤操作。上一篇文章中介绍的集合都提供了使用谓词对集合进行过滤的方法。 2、创建谓词之后,如果谓词中没有占位符,则可以直接使用NSPredicate的evaluateWithObject:方法计算谓词的结果,该结果总是一个BOOL值; 1 #import <Foundation * pred = [NSPredicate predicateWithFormat: 9 @"name like 's*'"]; 10 FKUser* user1 集合,返回集合中符合谓词条件的元素组成新集合 NSMutableArray提供了如下方法使用谓词来过滤集合: -(void)filteredUsingPredicate:(NSPredicate * * pred2 = [NSPredicate predicateWithFormat: 33 @"name CONTAINS '大王'"]; 34 //
[a-zA-Z]+$)[a-zA-Z0-9]{6,16}"; NSPredicate* predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES ; return isMatch; } + (BOOL)checkMoneyValue:(NSString*)money { NSString* pattern =@"^[1-9][0-9]*$"; NSPredicate [A-Za-z]{2,4}"; NSPredicate* predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",pattern]; + (BOOL)checkIDCard:(NSString*)idCard { NSString* pattern =@"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)"; NSPredicate ; NSPredicate* predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch
[A-Za-z]{2,4}"; NSPredicate emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE]; // NSPredicate regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM]; NSPredicate regextestcu = [NSPredicate :@"SELF MATCHES %@", CT]; // NSPredicate regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS]; // NSPredicate regextestphs1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS1
引言 利用KVC的 valueForKeyPath API、谓词的NSPredicate、数组的sortedArrayUsingComparator:API进行实现。 contentString); return contentString; } II 数组排序 2.1 对象数组按照日期重新分组 使用谓词进行数据分组 (数组元素为 自定义类型) iOS NSPredicate nil; // 4)按照日期进行分组 for (NSString *strDateCreated in arSorted) { predicate = [NSPredicate // 4)按照日期进行分组 for (NSString *strDateCreated in arSorted) { NSPredicate * predicate = [NSPredicate predicateWithFormat:@"createTime == %@", strDateCreated];
[A-Za-z]{2,4}”; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, emailRegex [A-Za-z]{2,4}”; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, emailRegex
133,153,180,181,189,177,1700 */ NSString *CT = @"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)"; NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE]; NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM]; NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU]; NSPredicate *regextestct = [NSPredicate predicateWithFormat [A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex
*pre = [NSPredicate predicateWithFormat:@"age < %d", 10]; deleRequest.predicate = pre; / //创建查询请求 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; NSPredicate *pre = [NSPredicate predicateWithFormat:@"sex = %@", @"帅哥"]; request.predicate = pre; / NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; //查询条件 NSPredicate *pre = [NSPredicate predicateWithFormat:@"sex = %@", @"美女"]; request.predicate = pre; // 从第几页开始显示
error; // 通过identifier获取事件 - (nullable EKEvent *)eventWithIdentifier:(NSString *)identifier; // 使用给定的NSPredicate 进行事件查询 - (NSArray<EKEvent *> *)eventsMatchingPredicate:(NSPredicate *)predicate; // 使用给定的NSPredicate进行事件枚举 - (void)enumerateEventsMatchingPredicate:(NSPredicate *)predicate usingBlock:(EKEventSearchCallback) block; // 构造NSPredicate查询对象 - (NSPredicate *)predicateForEventsWithStartDate:(NSDate *)startDate endDate 对象 @property(nonatomic, readonly) NSPredicate *contactPredicate; (adsbygoogle = window.adsbygoogle
正则语法努力学习中 一:使用NSPredicate(谓词)匹配 NSString *mail = @"zhangrongwuios@icloud.com"; NSString *regex = [A-Za-z]{2,4}"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex
*pre = [NSPredicate predicateWithFormat:@"age < %d", 10]; deleRequest.predicate = pre; / //创建查询请求 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; NSPredicate *pre = [NSPredicate predicateWithFormat:@"sex = %@", @"帅哥"]; request.predicate = pre; / NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; //查询条件 NSPredicate *pre = [NSPredicate predicateWithFormat:@"sex = %@", @"美女"]; request.predicate = pre; // 从第几页开始显示
[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex