谓词的使用 -ios
#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的更多相关文章
- iOS中的谓词(NSPredicate)使用
http://www.cocoachina.com/ios/20160111/14926.html 首先,我们需要知道何谓谓词,让我们看看官方的解释: The NSPredicate class is ...
- IOS开发之NSPredicate谓词的用法
编程的人员不管是上过大学还是从培训机构出来的或做后台的.前端的都应该SQL语句有所了解,我们知道,在SQL语句当中 where 条件表达式可以对二维关系表的数据做条件筛选.微软的C# .net中也实现 ...
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...
- iOS:转载:IOS谓词--NSPredicate
IOS谓词--NSPredicate 分类: IOS应用2013-02-19 17:24 6792人阅读 评论(1) 收藏 举报 Cocoa 提供了NSPredicate 用于指定过滤条件,谓词是指在 ...
- ios - 谓词的使用
谓词在搜索的时候非常管用.简单示例代码如下: 分类Person.h文件 #import <Foundation/Foundation.h> @interface Person : NSOb ...
- IOS 谓词
// 谓词 Person *person = [[Person alloc]init]; person.name = @"zhang san"; person.age = 20; ...
- iOS 谓词讲解
1.NSPredicate (1)比较运算符 1.比较运算符 > .< .== . >= .<= . != 运算符还可以跟逻辑运算符一起使用,&& , || ...
- iOS中谓词的使用
Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...
- iOS开发之谓词Predicate和对象数组的排序
我们在开发中经常使用的Predicate谓词,主要是正则表达式的使用,今天给大家简单的讲讲怎样去使用谓词. 因为内容比较简单,所以直接上代码展示: NSMutableArray *people_arr ...
随机推荐
- N个元素组成二叉树的种类
<算法>中的二叉查找树一节的一道习题. N个元素组成的二叉树固定一个根节点,这个根节点的左右子树组合数为(0,n-1),(1,n-2),(2,n-3)...(n-1,0),假设N个元素组成 ...
- wMy_Python ~储存相关~
str,int,list,tuple,dict 是类型调用之后会产生一个 实例 >>> brand=["李宁",'耐克','阿迪达斯','鱼C'] >> ...
- Apache 的启动/重启/停止
Task: Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 startor$ sudo /etc/init.d/apache2 star ...
- Qt中使用随机数
新建Empty qmake project,命名为UseRand UseRand.pro SOURCES += \ main.cpp QT += core main.cpp #include < ...
- Mac环境下装node.js,npm,express
1. 下载node.js for Mac 地址: http://nodejs.org/ 直接下载 pkg的,双击安装,一路点next,很容易就搞定了. 安装完会提醒注意 node和npm的路径是 /u ...
- Linux基础入门(新版)(实验五至实验八)
实验五 环境变量与文件查找 (环境变量的作用与用法,及几种搜索文件的方法) 一.环境变量 1.变量 (1)常变量与值是一对一的关系 (2)变量的作用域即变量的有效范围(比如一个函数中.一个源文 ...
- 利用windows系统ftp命令编写的BAT文件上传[转]
利用windows系统ftp命令编写的BAT文件上传[转] 利用windows系统ftp命令编写的BAT文件上传[转] 在开发中往往需要将本地的程序上传到服务器,而且用惯了linux命令的人来说.在w ...
- pscp使用详解 Windows与Linux文件互传工具
pscp使用详解 Windows与Linux文件互传工具 pscp使用方法详解:pscp是putty安装包所带的远程文件传输工具,是一款十分常见windows与linux系统之间文件互传的工具,使用方 ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- IFrame 获取内容
试试: iframe.contentwindow.document.documentElement.innerHTML document.getElementById("MyIFrame ...