#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. 摘自:java夜未眠之java学习之道

    目前Java可以说是产业界和学术界最热门的语言,许多读者都很急切想把Java学好.除非是武侠小说中的运功传送内力的方式,否则花上一段时间苦学是免不了的.花时间,不打紧,就是怕方法错误,事倍功半.我认为 ...

  2. oracle 10gwindow7安装添加内容

    F:\软件\database\stage\prereq\db\refhost.xml <!--Microsoft Windows 7 .Windows 8--> <OPERATING ...

  3. 从决策树学习谈到贝叶斯分类算法、EM、HMM --别人的,拷来看看

    从决策树学习谈到贝叶斯分类算法.EM.HMM     引言 最近在面试中,除了基础 &  算法 & 项目之外,经常被问到或被要求介绍和描述下自己所知道的几种分类或聚类算法(当然,这完全 ...

  4. Gradle: The New Android Build System

    Gradle: The New Android Build System Google selected Gradle as the foundation of the Android SDK bui ...

  5. selectors实现高并发

    1. 下面的例子,客户端给服务端发送消息,服务端把消息返回 server #!/usr/bin/env python import selectors import socket import tim ...

  6. C#基础知识学习

    C#基础知识整理 学习地址:http://blog.csdn.net/column/details/csarp.html

  7. 学习笔记:js、css、html判断浏览器的各种版本

    js.css.html判断浏览器的各种版本 (转载自:http://www.jb51.net/web/42244.html  版权归原作者所有) 利用正则表达式来判断ie浏览器版本 判断是否IE浏览器 ...

  8. magento和discuz(ucenter)整合集成开发思路

    discuz自带ucenter,主要就是用于和其他程序的通信.我们可以下载discuz的ucenter开发手册进行magento和discuz的集成.里面有一些ucenter的一些接口函数和参数说明, ...

  9. latex 小结

    1 no file .bbl http://blog.csdn.net/zhedasuiyuan/article/details/9223637 另外,你可能有多个 .bbl.目前的做法是,在控制台上 ...

  10. 【avalon】createMap

    /** * Creates a new object without a prototype. This object is useful for lookup without having to * ...