您的位置:首页 > 移动开发 > IOS开发

iOS中的谓词

2016-04-08 00:00 337 查看
谓词(保留所判断的,筛选用)

第一种使用谓词筛选的方法:

1

#import <Foundation/Foundation.h>

@interface man : NSObject
//第一步:新建一个man类,里面有name和age两种属性
@property (nonatomic,strong)NSString *name;
@property (nonatomic,assign)int age;

@end

2

#import "man.h"

@implementation man
//第二步:要打印对象的时候,其实也是用的description方法,这里我们要把它写出来,就可以看出打印的数据了,否则会是乱码
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %d", self.name,self.age];
}

@end

3

#import "ViewController.h"
//注意引入类man.h头文件
#import "man.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//第三步:创建一个数组,里面存有10个不同的属性
NSMutableArray *a = [[NSMutableArray alloc]initWithCapacity:10];
for (int i=0; i<10; i++) {
//使用类man创建一个m属性,注意引入头文件
man *m = [[man alloc]init];

if (i<5) {
//当循环变量i小于5的时候,m属性的名字叫qiang+i
m.name = [NSString stringWithFormat:@"qiang%d", i];
}else{
//当循环变量i大于5的时候,m属性的名字叫hong+i
m.name = [NSString stringWithFormat:@"hong%d", i];
}
//m的年龄是20+i
m.age = 20+i;
//最后将m属性添加到数组a中
[a addObject:m];
}

//创建一个谓词b,筛选条件是年龄小于25,谓词筛选条件可以是(&& || and等等,好多好多)
NSPredicate *b = [NSPredicate predicateWithFormat:@"age<25"];
//使用类man新建一个属性x,遍历数组a
for (man *x in a) {
//使用谓词b来筛选每一个遍历过的x值
BOOL yesOrNo = [b evaluateWithObject:x];
//判断并打印x值
if (yesOrNo) {
NSLog(@"%@", x);
}
}

}


第二种使用谓词筛选的方法(使用数组,比较简便):
#import "ViewController.h"
//注意引入类man.h头文件
#import "man.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//第三步:创建一个数组,里面存有10个不同的属性
NSMutableArray *a = [[NSMutableArray alloc]initWithCapacity:10];
for (int i=0; i<10; i++) {
//使用类man创建一个m属性,注意引入头文件
man *m = [[man alloc]init];

if (i<5) {
//当循环变量i小于5的时候,m属性的名字叫qiang+i
m.name = [NSString stringWithFormat:@"qiang%d", i];
}else{
//当循环变量i大于5的时候,m属性的名字叫hong+i
m.name = [NSString stringWithFormat:@"hong%d", i];
}
//m的年龄是20+i
m.age = 20+i;
//最后将m属性添加到数组a中
[a addObject:m];
}

//创建一个谓词b,筛选条件是年龄小于25,谓词筛选条件可以是(&& || and等等,好多好多)
NSPredicate *b = [NSPredicate predicateWithFormat:@"age<25"];

//第二种比较简便的方法:使用数组方法来筛选,不用for循环(在数组a中使用谓词b来筛选,将结果存到数组c中)
NSArray *c = [a filteredArrayUsingPredicate:b];

NSLog(@"%@", c);

}


例子:谓词
1
#import <Foundation/Foundation.h>

@interface student : NSObject

//第一步:先创建一个类studen,里面有两个属性name和age
@property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)NSInteger age;

//第二步:给类student分配内存空间和赋值(方法声明)
+(student *)allocWithname:(NSString *)name withAge:(NSInteger)age;
@end


2

#import "student.h"

@implementation student
//第三步:给类student分配内存空间和赋值(方法实现)
+(student *)allocWithname:(NSString *)name withAge:(NSInteger)age{
student *s = [[student alloc]init];

s.name = name;
s.age = age;

return s;
}

//第四步:如果想看到打印后的结果,就要写这个description方法,否则打印出来是乱码
-(NSString *)description{
NSString *n = [[NSString alloc] initWithFormat:@"age:%ld name:%@", _age, _name];
return n;
}

@end


3

#import "ViewController.h"
//引入类student.h的头文件
#import "student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//第五步:分别新建一个姓名库和年龄库的数组,存储各自的信息
NSArray *names = @[@"xiaoming", @"xiaohong", @"xiaoqiang", @"xiaogang", @"xiaoliu", @"xiaowang", @"xiaoma", ];
NSArray *ages= @[@20, @25, @30, @35, @40, @45, @50];

//新建一个数组stuArr
NSMutableArray *stuArr = [NSMutableArray array];

//遍历names数组(ages也一样)的成员个数
for (int i=0; i<names.count; i++) {
//使用类student创建一个临时成员stu,用来接收存储names数组和ages数组中的信息
student *stu = [student allocWithname:names[i] withAge:[ages[i]integerValue]];
//将stu添加到数组stuArr中
[stuArr addObject:stu];
}

//第六步:新建一个谓词a,设置筛选条件
NSPredicate *a = [NSPredicate predicateWithFormat:@"age<35"];
//在数组stuArr中使用谓词a来筛选,将结果存到数组b中
NSArray *b = [stuArr filteredArrayUsingPredicate:a];
NSLog(@"%@", b);

}


设置谓词
#import "ViewController.h"
#import "people.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray *a = [[NSMutableArray alloc]initWithCapacity:10];

for (int i=0; i<10; i++) {
people *p1 = [[people alloc]init];
if (i<5) {
p1.name = [NSString stringWithFormat:@"qiang%d", i];
}else{
p1.name = [NSString stringWithFormat:@"hong%d", i];
}
p1.age = 20+i;
[a addObject:p1];
}
NSLog(@"%@", a);

//-----------------设置谓词(运算符)---------------------

//小于
NSPredicate *b = [NSPredicate predicateWithFormat:@"age<27"];

//与 条件使用
NSPredicate *b1 = [NSPredicate predicateWithFormat:@"age<27 && age>23"];
NSPredicate *b2 = [NSPredicate predicateWithFormat:@"age<27 and age>23"];

//或 条件使用
NSPredicate *b3 = [NSPredicate predicateWithFormat:@"age<24 || age>27"];
NSPredicate *b4 = [NSPredicate predicateWithFormat:@"age<24 or age>27"];

//给占位符号传值方法
NSPredicate *b5 = [NSPredicate predicateWithFormat:@"age<%d", 25];

//等于条件(保留name = 'hong5)
NSPredicate *b6 = [NSPredicate predicateWithFormat:@"name = 'hong5'"];

//in条件使用
NSPredicate *b7 = [NSPredicate predicateWithFormat:@"name in {'qiang3','qiang4','hong5'}"];

//直接传递数组
NSArray *x = [NSArray arrayWithObjects:@"qiang4", @"hong5", @"hong6",  nil];
NSPredicate *b8 = [NSPredicate predicateWithFormat:@"name in %@", x];

//-----------------设置谓词(关键字)---------------------

//以什么开始设置(首字母)
NSPredicate *b9 = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'q'", x];

//以什么结束设置(尾字母)
NSPredicate *b10 = [NSPredicate predicateWithFormat:@"name ENDSWITH '7'", x];

//包含什么
NSPredicate *b11 = [NSPredicate predicateWithFormat:@"name CONTAINS 'o'"];

//模糊匹配
NSPredicate *b12 = [NSPredicate predicateWithFormat:@"name LIKE '*i*'"];

//外部传入的写法
NSString *j=[NSString stringWithFormat:@"name LIKE '*%@*'",@"o"];
NSPredicate *b13 = [NSPredicate predicateWithFormat:j];

NSArray *c = [a filteredArrayUsingPredicate:b13];
NSLog(@"ddddddd%@", c);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: