#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCopying>
@property(nonatomic,copy) NSString *name;
@property(nonatomic,retain) NSNumber *age;
-(void) setNewName:(NSString *) name;
@end
#import "Person.h"

@implementation Person:NSObject

- (id)copyWithZone:(NSZone *)zone{
Person *person=[[[self class] allocWithZone:zone] init];
person.name=_name;
person.age=_age;
return person;
}
-(void) setNewName:(NSString *) name{
self.name=name;
}
-(NSString *)description{
NSString *description=[NSString stringWithFormat:@"年龄:%@;名字:%@",_age,_name];
return description;
}
@end
//谓词用法 NSPredicate
NSMutableArray *array=[NSMutableArray array];
for (int i=0; i<10; i++) {
Person *person=[[Person alloc]init];
[person setAge:@(20+i)];
person.name=[NSString stringWithFormat:@"jage-%d",i];
[array addObject:person];
}
//大,小,等;等运算过滤
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"age<%d",25];
NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"age<23",23];
NSPredicate *predicate3=[NSPredicate predicateWithFormat:@"age<27 and age>25"];
NSPredicate *predicate4=[NSPredicate predicateWithFormat:@"age<27 && age>25"];
NSPredicate *predicate5=[NSPredicate predicateWithFormat:@"age<23 || age>26"];
NSPredicate *predicate6=[NSPredicate predicateWithFormat:@"name='jage-3'"];
//在某个里面
NSPredicate *predicate7=[NSPredicate predicateWithFormat:@"name in {'jage-1','jage-5'}"];
NSArray *inArray=@[@"jage-1",@"jage-4",@"jage-3"];
NSPredicate *predicate8=[NSPredicate predicateWithFormat:@"name in %@",inArray];
//已什么开头,注意加单引号
NSPredicate *predicate9=[NSPredicate predicateWithFormat:@"name BEGINSWITH 'jage'"];
//已什么结尾,注意加单引号
NSPredicate *predicate10=[NSPredicate predicateWithFormat:@"name ENDSWITH '-9'"];
//包含,注意加单引号
NSPredicate *predicate11=[NSPredicate predicateWithFormat:@"name CONTAINS '-3'"];
//like,注意加单引号
NSPredicate *predicate12=[NSPredicate predicateWithFormat:@"name LIKE 'jage-?'"];
NSPredicate *predicate13=[NSPredicate predicateWithFormat:@"name LIKE '*-2'"]; for (Person *person in array) {
if([predicate6 evaluateWithObject:person]){//逐个对象判断
NSLog(@"%@",person);
}
}
//对数组过滤
NSArray *filterArray=[array filteredArrayUsingPredicate:predicate13];
NSLog(@"%@",filterArray);

谓词的使用 -ios的更多相关文章

  1. iOS中的谓词(NSPredicate)使用

    http://www.cocoachina.com/ios/20160111/14926.html 首先,我们需要知道何谓谓词,让我们看看官方的解释: The NSPredicate class is ...

  2. IOS开发之NSPredicate谓词的用法

    编程的人员不管是上过大学还是从培训机构出来的或做后台的.前端的都应该SQL语句有所了解,我们知道,在SQL语句当中 where 条件表达式可以对二维关系表的数据做条件筛选.微软的C# .net中也实现 ...

  3. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...

  4. iOS:转载:IOS谓词--NSPredicate

    IOS谓词--NSPredicate 分类: IOS应用2013-02-19 17:24 6792人阅读 评论(1) 收藏 举报 Cocoa 提供了NSPredicate 用于指定过滤条件,谓词是指在 ...

  5. ios - 谓词的使用

    谓词在搜索的时候非常管用.简单示例代码如下: 分类Person.h文件 #import <Foundation/Foundation.h> @interface Person : NSOb ...

  6. IOS 谓词

    // 谓词 Person *person = [[Person alloc]init]; person.name = @"zhang san"; person.age = 20; ...

  7. iOS 谓词讲解

    1.NSPredicate (1)比较运算符 1.比较运算符 > .< .== . >= .<= . != 运算符还可以跟逻辑运算符一起使用,&&  ,  || ...

  8. iOS中谓词的使用

    Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...

  9. iOS开发之谓词Predicate和对象数组的排序

    我们在开发中经常使用的Predicate谓词,主要是正则表达式的使用,今天给大家简单的讲讲怎样去使用谓词. 因为内容比较简单,所以直接上代码展示: NSMutableArray *people_arr ...

随机推荐

  1. poj1014 dp 多重背包

    //Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...

  2. 一个有趣的IE内核检测网站

    http://se.360.cn/v5/iecoretest.html 该网站能有效检测您浏览器的内核,以及版本,操作系统. 找到这个网址,是因为最近解决WebBrowser自动调节IE版本功能时发现 ...

  3. PAT 05-树8 Huffman Codes

    以现在的生产力,是做不到一天一篇博客了.这题给我难得不行了,花了两天时间在PAT上还有测试点1没过,先写上吧.记录几个做题中的难点:1.本来比较WPL那块我是想用一个函数实现的,无奈我对传字符串数组无 ...

  4. C++数据结构之Queue(队列)

    Queue,队列,和我们日常生活中的队列是同样的规则,"先进先出",从尾入,从首出. Queue,主要有三种基本操作,append(添加元素至队尾):serve(队首元素出列):r ...

  5. PHP中函数的使用

    函数是一种可以在任何被需要的时候执行的代码块函数的定义    1.函数是一个被命名的独立的代码段    2.它执行特殊的任务    3.并可以给调用它的程序返回值 函数的优点:    1.提高程序的重 ...

  6. 【Zhejiang University PATest】02-1. Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. 【Tsinghua OJ】多米诺骨牌(domino)问题

    (domino.c/cpp)[问题描述] 小牛牛对多米诺骨牌有很大兴趣,然而她的骨牌比较特别,只有黑色和白色的两种.她觉 得如果存在连续三个骨牌是同一种颜色,那么这个骨牌排列便是不美观的.现在她有n个 ...

  8. 几次接触Collection排序使用总结

          初次接触Collection.sort()就是由鞠老师的作业4了解的,因为根据课程安排,这学期才开设java基础课,所有需要用到的的东西全是自学.而那次作业带给我最直观的感受就是:单纯的去 ...

  9. java IO流的体系结构图

    常用字节流字符流 字节流   InputStream                                                                           ...

  10. Redis - sort set类型操作

    sort set操作:sort set在set基础上增加了一列用于存储顺序序列设置操作:zadd key:        zadd key sortnum value        指定key增加元素 ...