#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. poj2631 树的直径 + bfs

    //Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...

  2. 另类加载dll---快捷方式启动参数

    http://blogs.360.cn/360safe/2014/08/29/cnc_trojan_and_fake_proto/ 原文: BMP1和BMP2是两个lnk文件(快捷方式),我们知道bm ...

  3. 关于RecyclerView中Viewholder和View的缓存机制的探究

    关于RecyclerView中Viewholder和View的缓存机制的探究 http://www.cnblogs.com/littlepanpc/p/4241575.html

  4. iOS计算缓存文件的大小

    //获取缓存文件路径 -(NSString *)getCachesPath{ // 获取Caches目录路径 NSArray *paths = NSSearchPathForDirectoriesIn ...

  5. WinFrm窗体的传值方式

    比较简单的方法: 一:1.定义两个窗体 2.在父窗体中加入子窗体的属性 public ChildFrm ChildFrm { get; set; } 3.加载的时候: private void Par ...

  6. 《MORE EFFECTIVE C++》条款20 条款21

    条款20 协助编译器实现返回值优化 当重载运算符的时候,比如+ - * / 这类运算符,该函数返回的值一定是个右值(即不能是引用),那么执行一次运算的开销可能会在临时对象上调用多次构造函数和析构函数, ...

  7. LeetCode Populating Next Right Pointers in Each Node (技巧)

    题意: 给一棵满二叉树,要求将每层的节点从左到右用next指针连起来,层尾指向NULL即可. 思路: 可以递归也可以迭代.需要观察到next的左孩子恰好就是本节点的右孩子的next啦. (1)递归:这 ...

  8. yii弹出层

    Yii弹出层,包装了JQuery的JDialog,使用很方便.Yii组件功能太强大,强大到无法自拔 $this->beginWidget('zii.widgets.jui.CJuiDialog' ...

  9. Mysql时间戳函数

    1.转换为时间戳 select unix_timestamp('2013-07-15 10-06-07') 如果参数为空,则为当前时间 2.转换为时间 select from_unixtime(tim ...

  10. sass中mixin常用的CSS3

    圆角border-radius @mixin rounded($radius){ -webkit-border-radius: $radius; -moz-border-radius: $radius ...