IOS的谓词语法小解
来源:互联网 发布:淘宝黑客 编辑:程序博客网 时间:2024/06/03 07:10
前言:
本篇博客,不在用太多的语言介绍,因为,介绍已经添加到代码中。现在。我们就直接上代码。如果,有不对的地方还请海涵。
第一、主代码:
//
// ViewController.m
// iOS的谓词
//
// Created by MAC on 16/7/20.
// Copyright © 2016年 NetworkCode小贱. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
typedef NSPredicate TP;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 谓词的语法
/*
@谓词的等于语法。
=、==(等价)
解释:@"A = B" Or @"A == B"是等式的左边等于右边
*/
/*
1、等于
=, ==
The left-hand expression is equal to the right-hand expression.
2、大于等于
>=, =>
The left-hand expression is greater than or equal to the right-hand expression.
3、小于等于
<=, =<
The left-hand expression is less than or equal to the right-hand expression.
4、大于
>
The left-hand expression is greater than the right-hand expression.
5、小于
<
The left-hand expression is less than the right-hand expression.
6、不等于
!=, <>
The left-hand expression is not equal to the right-hand expression.
7、之间
BETWEEN
The left-hand expression is between, or equal to either of, the values specified in the right-hand side.
*/
/**********************************************************/
NSArray * NumberCount =@[@10,@250,@100];
// TP * ZP = [TP predicateWithFormat:@"SELF = 250"];
TP * ZP = [TPpredicateWithFormat:@"SELF == 250"];
NSArray * ResultA = [NumberCountfilteredArrayUsingPredicate:ZP];
NSLog(@"%@",ResultA);
/*
输出的结果:
2016-07-20 12:01:53.895 iOS的谓词[9688:610934] (
250
)
*/
/**********************************************************/
NSString * NumberString =@"100";
// TP * ZP1 = [TP predicateWithFormat:@"SELF = 100"];
TP * ZP1 = [TPpredicateWithFormat:@"SELF = '100'"];
BOOL IS = [ZP1evaluateWithObject:NumberString];
NSLog(@"%d",IS);
/*
输出的结果:
2016-07-20 12:15:30.673 iOS的谓词[9795:642116] 0
// ****8
@"SELF = 100" 这种写法是不对的,得不到你想要的结果。
@"SELF = '100'" 这种写法是可以的。
@
*/
/**********************************************************/
NSArray * Number =@[@10,@250,@100];
// TP * ZP = [TP predicateWithFormat:@"SELF = 250"];
TP * ZPT = [TPpredicateWithFormat:@"SELF >20",@"SELF <350"];
NSArray * ResultB = [NumberfilteredArrayUsingPredicate:ZPT];
NSLog(@"%@",ResultB);
/*输出:
2016-07-20 13:08:48.539 iOS的谓词[9923:693447] (
250,
100
)
*/
/**********************************************************/
NSArray * NumberN =@[@10,@10,@100];
// TP * ZPN = [TP predicateWithFormat:@"SELF != 10"];
TP * ZPN = [TPpredicateWithFormat:@"SELF <> 10"];
NSArray * ResultN = [NumberNfilteredArrayUsingPredicate:ZPN];
NSLog(@"%@",ResultN);
/*
输出:
2016-07-20 13:18:57.888 iOS的谓词[10001:725480] (
100
)
*/
/**********************************************************/
NSArray * NumberBt =@[@10,@16,@200];
// TP * ZPN = [TP predicateWithFormat:@"SELF != 10"];
TP * ZPBt = [TPpredicateWithFormat:@"SELF BETWEEN {10,200}"];
NSArray * ResultBt = [NumberBtfilteredArrayUsingPredicate:ZPBt];
NSLog(@"%@",ResultBt);
/*
输出:
2016-07-20 13:37:04.110 iOS的谓词[10236:814634] (
10,
16,
200
)
注释:BETWEEN是区间,范围是:小于等于最大的,大于等于最小的。
@"SELF BETWEEN {10,200}" 等价于 @"SELF >=10 && SELF <=200"
*/
/***********************************************/
/****************************************/
/**********************************/
/****************************/
// 逻辑谓词语法
/*
1、与
AND, &&
Logical AND.
2、或
OR, ||
Logical OR.
3、非
NOT, !
Logical NOT.
*/
/**********************************************************/
NSArray * NumberL =@[@10,@16,@200];
// TP * ZPN = [TP predicateWithFormat:@"SELF != 10"];
TP * ZPL = [TPpredicateWithFormat:@"SELF < 100 OR SELF > 10"];
NSArray * ResultL = [NumberLfilteredArrayUsingPredicate:ZPL];
NSLog(@"%@",ResultL);
/*
输出:
2016-07-20 13:58:46.985 iOS的谓词[10419:894174] (
10,
16,
200
)
*/
/**********************************************************/
NSArray * NumberM =@[@10,@16,@200];
// TP * ZPN = [TP predicateWithFormat:@"SELF != 10"];
TP * ZPM = [TPpredicateWithFormat:@"SELF < 10 || SELF > 16"];
NSArray * ResultM = [NumberMfilteredArrayUsingPredicate:ZPM];
NSLog(@"%@",ResultM);
/*
输出:
2016-07-20 14:03:26.189 iOS的谓词[10473:915512] (
200
)
*/
/**********************************************************/
NSArray * NumberC =@[@10,@16,@200];
// TP * ZPMC = [TP predicateWithFormat:@"SELF < 100 AND SELF > 10"];
TP * ZPMC = [TPpredicateWithFormat:@"SELF < 100 && SELF > 10"];
NSArray * ResultML = [NumberCfilteredArrayUsingPredicate:ZPMC];
NSLog(@"%@",ResultML);
/*
输出:
2016-07-20 14:06:21.696 iOS的谓词[10502:931425] (
16
)
*/
/**********************************************************/
NSArray * NumberNO =@[@10,@16,@200];
TP * ZPNO = [TPpredicateWithFormat:@" NOT(SELF>16) || !(SELF<16)"];
NSArray * ResultNO = [NumberNOfilteredArrayUsingPredicate:ZPNO];
NSLog(@"%@",ResultNO);
/*
输出结果:
2016-07-21 09:04:52.470 iOS的谓词[826:91468] (
10,
16,
200
)
*/
/***********************************************/
/****************************************/
/**********************************/
/****************************/
// 字符串筛选的语法
/*
1、字符串是否以什么开头
BEGINSWITH
The left-hand expression begins with the right-hand expression.
2、字符串包含
CONTAINS
The left-hand expression contains the right-hand expression.
3、字符串以什么结尾
ENDSWITH
The left-hand expression ends with the right-hand expression.
4、谓词的模糊查询
LIKE
The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.
5、正则表达式的筛选
MATCHES
The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3 (for more details see the ICU User Guide for Regular Expressions).
6、判断两个是否相等
UTI-EQUALS
The left hand argument to this operator is an expression that evaluates to a universal type identifier (UTI) you want to match. The right hand argument is an expression that evaluates to a UTI. The comparison evaluates to TRUE if the UTI returned by the left hand expression equals the UTI returned by the right hand expression.
*/
/***********************************************/
NSArray * NumberCA =@[@"zhonghua",@"beijing",@"henan"];
TP * ZPCA = [TPpredicateWithFormat:@"self BEGINSWITH 'z' || self BEGINSWITH 'h'"];
NSArray * ResultCA = [NumberCAfilteredArrayUsingPredicate:ZPCA];
NSLog(@"%@",ResultCA);
/*
输出结果:
2016-07-21 09:14:25.281 iOS的谓词[905:115420] (
zhonghua,
henan
)
*/
/***********************************************/
NSArray * NumberContains =@[@"zhonghua",@"beijing",@"henan"];
TP * ZPContains = [TPpredicateWithFormat:@"self CONTAINS 'an' || self CONTAINS 'on'"];
NSArray * ResultContains = [NumberContainsfilteredArrayUsingPredicate:ZPContains];
NSLog(@"%@",ResultContains);
/*
输出:
2016-07-21 09:22:44.623 iOS的谓词[1075:148670] (
zhonghua,
henan
)
*/
/***********************************************/
NSArray * NumberCEnd =@[@"zhonghua",@"beijing",@"henan"];
TP * ZPCEnd = [TPpredicateWithFormat:@"self ENDSWITH 'g' || self ENDSWITH 'n'"];
NSArray * ResultCEnd = [NumberCEndfilteredArrayUsingPredicate:ZPCEnd];
NSLog(@"%@",ResultCEnd);
/*
输出:
2016-07-21 09:25:14.564 iOS的谓词[1103:160081] (
beijing,
henan
)
*/
/***********************************************/
NSArray * NumberCLike =@[@"zhonghua",@"beijing",@"henan"];
TP * ZPCLike = [TPpredicateWithFormat:@"self LIKE '*g' || self LIKE '*n'"];
NSArray * ResultCLike = [NumberCLikefilteredArrayUsingPredicate:ZPCLike];
NSLog(@"%@",ResultCLike);
/*
输出:
2016-07-21 09:36:29.632 iOS的谓词[1148:173842] (
beijing,
henan
)
*/
/***********************************************/
NSString * NumberCString =@"18811520397";
TP * ZPMATCHES = [TPpredicateWithFormat:@"self MATCHES %@",@"^1[3|4|5|7|8][0-9]\\d{8}$"];
BOOL ResultMATCHES= [ZPMATCHESevaluateWithObject:NumberCString];
NSLog(@"%d",ResultMATCHES);
/*
输出:
2016-07-21 10:09:31.577 iOS的谓词[1281:258855] 1
*/
/*
注意:@"self MATCHES '^1[3|4|5|7|8][0-9]\\d{8}$'"这样的写法是错的,语法是不错的。这样写得不到我们想要的结果。
*/
/***********************************************/
NSString * NumberUIT =@"18";
TP * ZPUIT = [TPpredicateWithFormat:@"self UTI-EQUALS '18'"];
BOOL ResultUIT= [ZPUITevaluateWithObject:NumberUIT];
NSLog(@"%d",ResultUIT);
/*
输出:
2016-07-21 10:44:34.285 iOS的谓词[1462:351511] 1
*/
/***********************************************/
/****************************************/
/**********************************/
/****************************/
// 谓词集合语法
/*
1、一些、任意
ANY, SOME
Specifies any of the elements in the following expression. For example ANY children.age < 18.
2、所有
ALL
Specifies all of the elements in the following expression. For example ALL children.age < 18.
3、逻辑非
NONE
Specifies none of the elements in the following expression. For example, NONE children.age < 18. This is logically equivalent to NOT (ANY ...).
4、交集
IN
Equivalent to an SQL IN operation, the left-hand side must appear in the collection specified by the right-hand side.
*/
/***********************************************/
NSArray *words =@[
@[@10,@20],
@[@10,@50, @100],
@[@10,@60]
];
//TP * ZPANY = [TP predicateWithFormat:@"ANY SELF == %@",@10]; //等价于
TP * ZPANY = [TPpredicateWithFormat:@"SOME SELF == %@",@10];
NSArray * ResultANY= [wordsfilteredArrayUsingPredicate:ZPANY];
NSLog(@"%@",ResultANY);
/*
输出:
2016-07-21 13:38:16.948 iOS的谓词[1978:511518] (
(
10,
20
),
(
10,
50,
100
),
(
10,
60
)
)
注释: ALL和AND/SOME用法一样
*/
/***********************************************/
NSArray *Numberwords =@[
@[@10,@20],
@[@10,@50, @100],
@[@10,@60]
];
// TP * ZPNONE = [TP predicateWithFormat:@"NONE SELF == %@",@50];//等价于
TP * ZPNONE = [TPpredicateWithFormat:@"NONE SELF == 50"];
NSArray * ResultNONE= [NumberwordsfilteredArrayUsingPredicate:ZPNONE];
NSLog(@"%@",ResultNONE);
/*
输出:
2016-07-21 13:50:22.578 iOS的谓词[2086:547199] (
(
10,
20
),
(
10,
60
)
)
注释:
@"NONE SELF == %d",@50 这样的写法是错误的。结果是全部ALL。
*/
/***********************************************/
NSArray *NumberIN =@[
@[@10,@20],
@[@10,@50, @100],
@[@10,@60]
];
TP * ZPIN = [TPpredicateWithFormat:@"ALL SELF IN %@",@[@10,@60]];
//TP * ZPIN = [TP predicateWithFormat:@" SELF IN %@",@[@10,@60]]; //错误
NSArray * ResultIN= [NumberINfilteredArrayUsingPredicate:ZPIN];
NSLog(@"%@",ResultIN);
/*
输出:
2016-07-21 14:04:41.829 iOS的谓词[2270:602269] (
(
10,
60
)
)
注释:
@" SELF IN %@",@[@10,@60] 是错误的得不到我们想要的结果。
*/
/***********************************************/
/****************************************/
/**********************************/
/****************************/
// 一些谓词关键词意思
/*
1、假
FALSE, NO
Logical false.
2、真
TRUE, YES
Logical true.
3、空
NULL, NIL
A null value.
4、对象本身
SELF
Represents the object being evaluated.
5、字符串
"text"
A character string.
'text'
A character string.
6、数组(谓词中)
Comma-separated literal array
For example, { 'comma', 'separated', 'literal', 'array' }.
7.浮点数
Standard integer and fixed-point notations
For example, 1, 27, 2.71828, 19.75.
Floating-point notation with exponentiation
8、e函数
For example, 9.2e-5.
9、十六进制
0x
Prefix used to denote a hexadecimal digit sequence.
10、八进制
0o
Prefix used to denote an octal digit sequence.
11、二进制
0b
Prefix used to denote a binary digit sequence.
*/
/***********************************************/
/****************************************/
/**********************************/
/****************************/
// 所有关键词
/*
AND(与), OR(或), IN(交集), NOT(非), ALL(所有),(ANY, SOME)(指定区域), NONE(补集), LIKE(模糊匹配), CASEINSENSITIVE, CI, MATCHES(正则), CONTAINS(包含),
BEGINSWITH(以什么开头), ENDSWITH(以什么结尾), BETWEEN(在什么什么之间),(NULL, NIL)(空对象), SELF(对象本身),(TRUE, YES)(真),(FALSE, NO)(假), FIRST(第一), LAST(最后),
SIZE(大小), ANYKEY(。。。), SUBQUERY, CAST, TRUEPREDICATE(返回为真得谓词), FALSEPREDICATE(返回为假的谓词), UTI-CONFORMS-TO, UTI-EQUALS(等于)
*/
/***********************************************/
/****************************************/
/**********************************/
/****************************/
//NSPredicate 的一些方法解释
/*
第一个方法:
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;
*/
NSArray * CityNameArray =@[@[@"henam",@"beijing"],@[@"xian"],@[@"qinghai",@"xuchang",@"luhe"],@[@"kaifeng"]];
NSArray * CityNameArray2 =@[@[@"henan",@"heibie"],@[@"xiangtai"],@[@"qinghaihu",@"xuchangshi",@"luhe"],@[@"kaifengfu"]];
// 模糊匹配
TP * MoHu = [TPpredicateWithFormat:@"ANY SELF CONTAINS %@",@"henam"];
NSArray * MoHuARRay = [CityNameArrayfilteredArrayUsingPredicate:MoHu];
NSLog(@"%@",MoHuARRay);
/*
输出:
2016-07-21 14:58:36.400 iOS的谓词[2604:764411] (
(
henam,
beijing
)
)
*/
// 精确匹配
MoHu = [TPpredicateWithFormat:@"ANY SELF IN %@",@"luhe"];
NSArray * JingQueA = [CityNameArrayfilteredArrayUsingPredicate:MoHu];
NSLog(@"%@",JingQueA);
/*
输出:
2016-07-21 15:01:09.683 iOS的谓词[2643:776525] (
(
qinghai,
xuchang,
luhe
)
)
*/
// 多个匹配
MoHu = [TPpredicateWithFormat:@"ANY SELF IN %@",@[@"henan",@"beijing"]];
NSArray * JingQueM = [CityNameArrayfilteredArrayUsingPredicate:MoHu];
NSLog(@"%@",JingQueM);
/*
输出:
2016-07-21 15:03:20.953 iOS的谓词[2689:787869] (
(
henam,
beijing
)
)
*/
// 不包含某个字段的匹配
MoHu = [TPpredicateWithFormat:@"NONE SELF CONTAINS %@",@"henan"];
NSArray * NOA = [CityNameArrayfilteredArrayUsingPredicate:MoHu];
NSLog(@"%@",NOA);
/*
输出:
2016-07-21 15:05:00.479 iOS的谓词[2721:795366] (
(
xian
),
(
qinghai,
xuchang,
luhe
),
(
kaifeng
)
)
*/
//断言(嵌套数组是精确匹配)
MoHu = [TPpredicateWithFormat:@"ANY %@ CONTAINS[cd] %@",CityNameArray2,@"henan"];
BOOL DY = [MoHuevaluateWithObject:CityNameArray2];
NSLog(@"%d",DY);
/*
输出:
2016-07-21 15:13:20.690 iOS的谓词[2872:831792] 1
*/
// 评估(单个数组的评估)
MoHu = [TPpredicateWithFormat:@"ANY %@ CONTAINS[cd] %@",CityNameArray2[0],@"henan"];
BOOL SingolA = [MoHuevaluateWithObject:CityNameArray2];
NSLog(@"%d",SingolA);
/*
输出;
2016-07-21 15:21:03.686 iOS的谓词[2942:867601] 1
*/
/*
第二个方法:
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(nullable NSArray *)arguments;
*/
MoHu = [TPpredicateWithFormat:@"ALL SELF IN %@"argumentArray:@[CityNameArray2.firstObject]];
NSArray * SingolAS = [CityNameArray2filteredArrayUsingPredicate:MoHu];
NSLog(@"%@",SingolAS);
/*
2016-07-21 15:28:10.374 iOS的谓词[3075:900294] (
(
henan,
heibie
)
)
注释:
MoHu = [TP predicateWithFormat:@"ALL SELF IN %@" argumentArray:CityNameArray2.firstObject];这种写法不对
*/
/*
第三个方法:
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat arguments:(va_list)argList;
*/
[selfpredicateWithFormatArray:CityNameArray2andArgList:@"h",@"n"];
/*
输出:
2016-07-21 15:44:58.286 iOS的谓词[3265:954041] (
(
henan,
heibie
),
(
xiangtai
),
(
qinghaihu,
xuchangshi,
luhe
),
(
kaifengfu
)
)
注意:
@"h",@"n" 是或的条件
*/
/*
第四个方法:
+ (NSPredicate *)predicateWithValue:(BOOL)value; // return predicates that always evaluate to true/false
*/
MoHu = [TPpredicateWithValue:YES];
NSArray * SingolYES= [CityNameArray2filteredArrayUsingPredicate:MoHu];
NSLog(@"%@",SingolYES);
/*
2016-07-21 15:50:30.614 iOS的谓词[3314:975269] (
(
henan,
heibie
),
(
xiangtai
),
(
qinghaihu,
xuchangshi,
luhe
),
(
kaifengfu
)
)
注意:这是一个一直返回真的谓词;还有一个NO,是一直返回假的谓词。
*/
/*
第五个方法:
+ (NSPredicate*)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary<NSString *, id> * __nullable bindings))block NS_AVAILABLE(10_6, 4_0);
*/
MoHu = [TPpredicateWithBlock:^BOOL(id _Nonnull evaluatedObject,NSDictionary<NSString *,id> *_Nullable bindings) {
// 判断参数是否存在
if (bindings) {
return [[evaluatedObjectobjectAtIndex:0]isEqualToString:bindings[@"key"]];
}else{
return [[evaluatedObjectobjectAtIndex:0]isEqualToString:@"henan"];
}
}];
NSLog(@"%@",[MoHupredicateWithSubstitutionVariables:@{@"key":@"henan"}]);
NSArray * SingolBl= [CityNameArray2filteredArrayUsingPredicate:MoHu];
NSLog(@"%@",SingolBl);
/*
第五个方法:
- (instancetype)predicateWithSubstitutionVariables:(NSDictionary<NSString *, id> *)variables; // substitute constant values for variables
*/
MoHu = [NSPredicatepredicateWithFormat:@"ANY SELF IN $strVar"];
NSPredicate *subPre = [MoHupredicateWithSubstitutionVariables:@{@"strVar":@"henan"}];
NSLog(@"%@",[CityNameArray2filteredArrayUsingPredicate:subPre]);
/*
输出:
2016-07-21 17:05:09.148 iOS的谓词[3560:1286820] (
(
henan,
heibie
)
)
*/
/*
可变数组的查询
*/
NSMutableArray * MutableArray = [NSMutableArrayarrayWithObjects:@[@10,@20],@[@10,@80,@30],nil];
// 多个匹配
MoHu = [TPpredicateWithFormat:@"ANY SELF IN %@",@[@10,@20]];
[MutableArray filterUsingPredicate:MoHu];
NSLog(@"%@",MutableArray);
/***********************************************/
/****************************************/
/**********************************/
/****************************/
// 其他方法和技巧
/*
动态属性名
假如你的代码如下
NSPredicate *p = [NSPredicate predicateWithFormat:@"name = %@", @"name1"];
显然代码没有任何问题,但是这个不是最好的写法我建议如下写法:
NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
@"name1", @"NAME",nil];
NSPredicate *pre=[preTemplate predicateWithSubstitutionVariables: dic];
这样看上去可能会让代码逻辑更清晰。
当过滤条件字段都是动态的时候
NSString *key = @"name";
NSString *value = @"name1";
NSPredicate *p = [NSPredicate predicateWithFormat:@"%@ = %@", key, value];
然后当你执行到第三行的时候代码就会报错!
逻辑上没错误啊!!!为什么会出错呢?
NSPredicate要自动添加引号,所以最后得到的格式应该是@"'name' = 'name1'"。明显不对。要做的就是:
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K = %@", key, value];
*/
// Do any additional setup after loading the view, typically from a nib.
}
-(void)predicateWithFormatArray:(NSArray*)array andArgList:(NSString*)args, ...{
va_list MList ;
va_start(MList, args);
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"ANY SELF CONTAINS %@"arguments:MList];
va_end(MList);
NSLog(@"%@",[arrayfilteredArrayUsingPredicate:predicate]);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- IOS的谓词语法小解
- ios 谓词的使用
- IOS 谓词的理解
- iOS之谓词的使用
- IOS之谓词的用法
- iOS谓词的简单使用
- IOS中谓词的使用
- iOS 谓词
- iOS 谓词
- ios 谓词
- iOS 中谓词的研究心得
- IOS学习笔记:谓词的使用
- iOS---NSPredicate谓词的用法 数组过滤
- iOS中谓词的使用(一)
- iOS谓词的使用 (NSPredicate)
- ios中 难点小解
- ios 之 autoresizing小解
- ios 之 autoresizing小解
- 评价UML工具
- linux命令提示符显示当前完整路径
- 线程安全与共享资源
- DOM对象模型高级编程(增查)
- 遍历Map的四种方法
- IOS的谓词语法小解
- 解决MYSQL连接慢问题
- 电脑硬件名称 中英文对照
- Android_Service多线程断点下载
- cocos2dx-js 绑定c++
- Eclipse 控制台中文乱码
- 内置对象学习笔记
- 数据库中sum和count的区别
- BootStrap 工具提示 弹出框 警告框