CoreData中NSpredicate的使用和延伸
2015-05-24 21:47
183 查看
NSPredicate在CoreData中常用作查询使用,相当于sql语句中的where查询子句。
最常用的方法为:
view
sourceprint?
比如我们要查询student表中name=“jjy”的信息,我们可以这样去用NSPredicate
view
sourceprint?
得到的就是名称为jjy的个人信息。
当然了这个还有其他用处,在网上看大针对其他用法的总结,在这借用过来,分享给大家:
Format:
(1)比较运算符>,<,==,>=,<=,!=
可用于数值及字符串
例:@"number >100"
(2)范围运算符:IN、BETWEEN
例:@"number BETWEEN {1,5}"
@"address IN {'shanghai','beijing'}"
(3)字符串本身:SELF
例:@“SELF == ‘APPLE’"
(4)字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
例:@"name CONTAIN[cd] 'ang'" //包含某个字符串
@"name BEGINSWITH[c] 'sh'" //以某个字符串开头
@"name ENDSWITH[d] 'ang'" //以某个字符串结束
注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
(5)通配符:LIKE
例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd].
@"name LIKE[cd] '???er*'"
(6)正则表达式:MATCHES
例:NSString *regex = @"^A.+e$";//以A开头,e结尾
@"name MATCHES %@",regex
实际应用:
(1)对NSArray进行过滤
view
sourceprint?
(2)判断字符串首字母是否为字母:
view
sourceprint?
(3)字符串替换:
view
sourceprint?
(4)截取字符串如下:
view
sourceprint?
(5)判断手机号码,电话号码函数
view
sourceprint?
(6)邮箱验证、电话号码验证:
view
sourceprint?
(7)NSDate进行筛选
view
sourceprint?
最常用的方法为:
view
sourceprint?
1.
NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...];
比如我们要查询student表中name=“jjy”的信息,我们可以这样去用NSPredicate
view
sourceprint?
01.
NSEntityDescription * emEty = [NSEntityDescription entityForName:@
"student"
inManagedObjectContext:self.managedObjectContext];
02.
NSFetchRequest *frq = [[NSFetchRequest alloc]init];
03.
04.
[frq setEntity:emEty];
05.
06.
NSPredicate * cdt = [NSPredicate predicateWithFormat:@
"name= %@"
,@
"jjy"
];
07.
08.
[frq setPredicate:cdt];
09.
10.
NSArray *objs =[self.managedObjectContext executeFetchRequest:frq error:nil];
得到的就是名称为jjy的个人信息。
当然了这个还有其他用处,在网上看大针对其他用法的总结,在这借用过来,分享给大家:
Format:
(1)比较运算符>,<,==,>=,<=,!=
可用于数值及字符串
例:@"number >100"
(2)范围运算符:IN、BETWEEN
例:@"number BETWEEN {1,5}"
@"address IN {'shanghai','beijing'}"
(3)字符串本身:SELF
例:@“SELF == ‘APPLE’"
(4)字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
例:@"name CONTAIN[cd] 'ang'" //包含某个字符串
@"name BEGINSWITH[c] 'sh'" //以某个字符串开头
@"name ENDSWITH[d] 'ang'" //以某个字符串结束
注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
(5)通配符:LIKE
例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd].
@"name LIKE[cd] '???er*'"
(6)正则表达式:MATCHES
例:NSString *regex = @"^A.+e$";//以A开头,e结尾
@"name MATCHES %@",regex
实际应用:
(1)对NSArray进行过滤
view
sourceprint?
1.
NSArray *array = [[NSArray alloc]initWithObjects:@
"beijing"
,@
"shanghai"
,@
"guangzou"
,@
"wuhan"
, nil];
2.
NSString *string = @
"ang"
;
3.
NSPredicate *pred = [NSPredicate predicateWithFormat:@
"SELF CONTAINS %@"
,string];
4.
NSLog(@
"%@"
,[array filteredArrayUsingPredicate:pred]);
(2)判断字符串首字母是否为字母:
view
sourceprint?
1.
NSString *regex = @
"[A-Za-z]+"
;
2.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@
"SELF MATCHES %@"
,
regex];
3.
4.
if
([predicate evaluateWithObject:aString]) {
5.
}
(3)字符串替换:
view
sourceprint?
01.
NSError*
error = NULL;
02.
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@
"(encoding=\")[^\"]+(\")"
03.
options:
0
04.
error:&error];
05.
NSString* sample = @
"<xml encoding=\"abc\"></xml><xml encoding=\"def\"></xml><xml encoding=\"ttt\"></xml>"
;
06.
NSLog(@
"Start:%@"
,sample);
07.
NSString* result = [regex stringByReplacingMatchesInString:sample
08.
options:
0
09.
range:NSMakeRange(
0
, sample.length)
10.
withTemplate:@
"$1utf-8$2"
];
11.
NSLog(@
"Result:%@"
, result);
(4)截取字符串如下:
view
sourceprint?
01.
//组装一个字符串,需要把里面的网址解析出来
02.
NSString *urlString=@
"<meta/><link/><title>1Q84 BOOK1</title></head><body>"
;
03.
04.
//NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个
05.
NSError
*error;
06.
07.
//http+:[^\\s]* 这个表达式是检测一个网址的。(?<=title\>).*(?=</title)截取html文章中的<title></title>中内文字的正则表达式
08.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@
"(?<=title\\>).*(?=</title)"
options:
0
error:&error];
09.
10.
if
(regex != nil) {
11.
NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:
0
range:NSMakeRange(
0
,
[urlString length])];
12.
13.
if
(firstMatch) {
14.
NSRange resultRange = [firstMatch rangeAtIndex:
0
];
15.
16.
//从urlString当中截取数据
17.
NSString
*result=[urlString substringWithRange:resultRange];
18.
//输出结果
19.
NSLog(@
"->%@<-"
,result);
20.
}
21.
22.
}
(5)判断手机号码,电话号码函数
view
sourceprint?
01.
//组装一个字符串,需要把里面的网址解析出来
02.
NSString *urlString=@
"<meta/><link/><title>1Q84 BOOK1</title></head><body>"
;
03.
04.
//NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个
05.
NSError
*error;
06.
07.
//http+:[^\\s]* 这个表达式是检测一个网址的。(?<=title\>).*(?=</title)截取html文章中的<title></title>中内文字的正则表达式
08.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@
"(?<=title\\>).*(?=</title)"
options:
0
error:&error];
09.
10.
if
(regex != nil) {
11.
NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:
0
range:NSMakeRange(
0
,
[urlString length])];
12.
13.
if
(firstMatch) {
14.
NSRange resultRange = [firstMatch rangeAtIndex:
0
];
15.
16.
//从urlString当中截取数据
17.
NSString
*result=[urlString substringWithRange:resultRange];
18.
//输出结果
19.
NSLog(@
"->%@<-"
,result);
20.
}
21.
22.
}
23.
24.
(
5
)判断手机号码,电话号码函数
25.
[cpp] view plaincopy
26.
// 正则判断手机号码地址格式
27.
- (BOOL)isMobileNumber:(NSString *)mobileNum
28.
{
29.
/**
30.
* 手机号码
31.
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
32.
* 联通:130,131,132,152,155,156,185,186
33.
* 电信:133,1349,153,180,189
34.
*/
35.
NSString * MOBILE = @
"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"
;
36.
/**
37.
10 * 中国移动:China Mobile
38.
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
39.
12 */
40.
NSString * CM = @
"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
;
41.
/**
42.
15 * 中国联通:China Unicom
43.
16 * 130,131,132,152,155,156,185,186
44.
17 */
45.
NSString * CU = @
"^1(3[0-2]|5[256]|8[56])\\d{8}$"
;
46.
/**
47.
20 * 中国电信:China Telecom
48.
21 * 133,1349,153,180,189
49.
22 */
50.
NSString * CT = @
"^1((33|53|8[09])[0-9]|349)\\d{7}$"
;
51.
/**
52.
25 * 大陆地区固话及小灵通
53.
26 * 区号:010,020,021,022,023,024,025,027,028,029
54.
27 * 号码:七位或八位
55.
28 */
56.
//
NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
57.
58.
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@
"SELF MATCHES %@"
,
MOBILE];
59.
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@
"SELF MATCHES %@"
,
CM];
60.
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@
"SELF MATCHES %@"
,
CU];
61.
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@
"SELF MATCHES %@"
,
CT];
62.
63.
if
(([regextestmobile evaluateWithObject:mobileNum] == YES)
64.
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
65.
|| ([regextestct evaluateWithObject:mobileNum] == YES)
66.
|| ([regextestcu evaluateWithObject:mobileNum] == YES))
67.
{
68.
if
([regextestcm
evaluateWithObject:mobileNum] == YES) {
69.
NSLog(@
"China Mobile"
);
70.
}
else
if
([regextestct
evaluateWithObject:mobileNum] == YES) {
71.
NSLog(@
"China Telecom"
);
72.
}
else
if
([regextestcu
evaluateWithObject:mobileNum] == YES) {
73.
NSLog(@
"China Unicom"
);
74.
}
else
{
75.
NSLog(@
"Unknow"
);
76.
}
77.
78.
return
YES;
79.
}
80.
else
81.
{
82.
return
NO;
83.
}
84.
}
(6)邮箱验证、电话号码验证:
view
sourceprint?
01.
//是否是有效的正则表达式
02.
03.
+(BOOL)isValidateRegularExpression:(NSString *)strDestination byExpression:(NSString *)strExpression
04.
05.
{
06.
07.
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@
"SELF MATCHES %@"
,
strExpression];
08.
09.
return
[predicate
evaluateWithObject:strDestination];
10.
11.
}
12.
13.
//验证email
14.
+(BOOL)isValidateEmail:(NSString
*)email {
15.
16.
NSString *strRegex = @
"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}"
;
17.
18.
BOOL
rt = [CommonTools isValidateRegularExpression:email byExpression:strRegex];
19.
20.
return
rt;
21.
22.
}
23.
24.
//验证电话号码
25.
+(BOOL)isValidateTelNumber:(NSString
*)number {
26.
27.
NSString *strRegex = @
"[0-9]{1,20}"
;
28.
29.
BOOL
rt = [CommonTools isValidateRegularExpression:number byExpression:strRegex];
30.
31.
return
rt;
32.
33.
}
(7)NSDate进行筛选
view
sourceprint?
01.
//日期在十天之内:
02.
NSDate
*endDate = [[NSDate date] retain];
03.
NSTimeInterval
timeInterval= [endDate timeIntervalSinceReferenceDate];
04.
timeInterval -=
3600
*
24
*
10
;
05.
NSDate
*beginDate = [[NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval] retain];
06.
//对coredata进行筛选(假设有fetchRequest)
07.
NSPredicate *predicate_date =
08.
[NSPredicate predicateWithFormat:@
"date >= %@ AND date <= %@"
,
beginDate,endDate];
09.
10.
[fetchRequest
setPredicate:predicate_date];
11.
//释放retained的对象
12.
[endDate
release];
13.
[beginDate
release];
相关文章推荐
- CoreData 中使用 NSPredicate 过滤掉空值
- CoreData使用方法三: NSPredicate在CoreData中的使用
- CoreData用法三: NSPredicate在CoreData中的使用
- iOS coreData使用遇到的问题
- iOS:CoreData数据库的使用四(数据库和UITableViewController以及NSFetchedResultsController一起使用)
- ios coreData使用
- ios中的coredata的使用
- 使用CoreData数据库框架的简单方式
- [Cocoa]深入浅出 Cocoa 之 Core Data(3)- 使用绑定
- IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
- (CoreData的使用,增删改查)
- IOS-Coredata 核心数据库的基础使用
- iOS CoreData介绍和使用(以及一些注意事项)
- CoreData的简单使用
- IOS数据存储之CoreData使用优缺点
- ios中的coredata的使用
- IOS成长之路-Core Data使用时用到的类--微解
- coredata简单使用,插删改查
- [Cocoa]深入浅出 Cocoa 之 Core Data(4)- 使用绑定
- iOS开发--数据库管理CoreData的使用