NSpredicate 常用方法

    // 谓词的条件查询 > 、< 、==、!=
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"age > 20"];
NSArray *arr1 = [stuArray filteredArrayUsingPredicate:predicate1];
NSLog(@"arr1 = %@",arr1); //使用表达式
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"age > 20 && height > 170"];
NSArray *arr2 = [stuArray filteredArrayUsingPredicate:predicate2];
NSLog(@"arr2 = %@",arr2); // 字符串作为条件要使用单引号‘’,name == 'lisi'
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name == 'lisi'"];
NSArray *arr3 = [stuArray filteredArrayUsingPredicate:predicate3];
NSLog(@"arr3 = %@",arr3); // 使用占位符
NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"age == %ld",];
NSArray *arr4 = [stuArray filteredArrayUsingPredicate:predicate4];
NSLog(@"arr4 = %@",arr4); // IN {'lisi','wangwu'} 在包含的条件内
NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"height IN {165,180}"];
NSArray *arr5 = [stuArray filteredArrayUsingPredicate:predicate5];
NSLog(@"arr5 = %@",arr5); //BEGINSWITH 以什么字符串开头
NSPredicate *predicate6 = [NSPredicate predicateWithFormat:@"name BEGINSWITH[c] 'R'"];
NSArray *arr6 = [stuArray filteredArrayUsingPredicate:predicate6];
NSLog(@"arr6 = %@",arr6); // ENDSWITH 以什么字符串结尾
NSPredicate *predicate7 = [NSPredicate predicateWithFormat:@"name ENDSWITH 'e'"];
NSArray *arr7 = [stuArray filteredArrayUsingPredicate:predicate7];
NSLog(@"arr7 = %@",arr7); // CONTAINS 包含某个字符串
NSPredicate *predicate8 = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
NSArray *arr8 = [stuArray filteredArrayUsingPredicate:predicate8];
NSLog(@"arr8 = %@",arr8); /* LIKE: 1、 LIKE '*a*' 包含某个字符串
2、 LIKE 'j*' 以某个字符串开头
3、 LIKE '*e 以某个字符串结尾
4、 LIKE '?a*' 第二个字符串是 a 的
5、 LIKE[c] 'r*' [c] 表示忽略大小写 *: 星号表示任意个字符位,?问号,表示一个字符位,[c] 表示忽略大小写 */ NSPredicate *predicate9 = [NSPredicate predicateWithFormat:@"name LIKE '*a*'"];
NSArray *arr9 = [stuArray filteredArrayUsingPredicate:predicate9];
NSLog(@"arr9 = %@",arr9); NSPredicate *predicate10 = [NSPredicate predicateWithFormat:@"name LIKE[c] 'r*'"];
NSArray *arr10 = [stuArray filteredArrayUsingPredicate:predicate10];
NSLog(@"arr10 = %@",arr10); NSPredicate *predicate11 = [NSPredicate predicateWithFormat:@"name LIKE '*e'"];
NSArray *arr11 = [stuArray filteredArrayUsingPredicate:predicate11];
NSLog(@"arr11 = %@",arr11); NSPredicate *predicate12 = [NSPredicate predicateWithFormat:@"name LIKE '?a*'"];
NSArray *arr12 = [stuArray filteredArrayUsingPredicate:predicate12];
NSLog(@"arr12 = %@",arr12); // NSMutableArray 使用 filterUsingPredicate,不会返回一个新数组,而是直接覆盖原来的可变数组
NSPredicate *predicate13 = [NSPredicate predicateWithFormat:@"name LIKE '?a*'"];
[stuArray filterUsingPredicate:predicate13];
NSLog(@"stuArray = %@",stuArray);

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_NSPredicate

iOS-OC-基础-NSPredicate常用方法的更多相关文章

  1. iOS 阶段学习第11天笔记(OC基础知识)

    iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import  用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...

  2. iOS开发OC基础:Xcode中常见英文总结,OC常见英文错误

    在开发的过程中难免会遇到很多的错误,可是当看到系统给出的英文时,又不知道是什么意思.所以这篇文章总结了Xcode中常见的一些英文单词及词组,可以帮助初学的人快速了解给出的提示.多练习,就肯定能基本掌握 ...

  3. iOS 面试基础题目

    转载: iOS 面试基础题目 题目来自博客:面试百度的记录,有些问题我能回答一下,不能回答的或有更好的回答我放个相关链接供参考. 1面 Objective C runtime library:Obje ...

  4. OC基础笔记目录

    OC基础(1) Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 OC基础(2) 类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 OC基础(3) 对象 ...

  5. ios+oc面试题

    ios+oc面试题     浅复制和深复制的区别?//浅拷贝和深拷贝答案:浅层复制(copy):只复制指向对象的指针,而不复制引用对象本身.//通过对象的指针来访问这个对象深层复制(mutableCo ...

  6. OC基础16:复制对象

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.浅复制即是指针赋值,复制对象的修改会影 ...

  7. OC基础8:分类和协议

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于分类(category): (1) ...

  8. 【IOS学习基础】NSObject.h学习

    一.<NSObject>协议和代理模式 1.在NSObject.h头文件中,我们可以看到 // NSObject类是默认遵守<NSObject>协议的 @interface N ...

  9. OC基础回想(十二)协议

    在OC基础(十一)中我们讨论了类别和非正式协议的奇异之处.在使用非正式协议时.能够仅仅实现你想要获得响应的方法.也不必在对象中声明不论什么内容来表示该对象可用作托付对象. 全部这些任务能够用最少的代码 ...

  10. 一些iOS面试基础题总结

    一些iOS面试基础题总结 目录 多线程 AutoLayout objc_msgSend Runtime 消息转发 Category NSObject 与 objc_class Runloop Auto ...

随机推荐

  1. C# string的一些函数

    创建string: string (char[])      使用指定的字符串数组构建一个新的string对象 Copy(string) 使用指定的string构建一个新的string对象 比较函数: ...

  2. Java中构造方法的执行顺序

    一.先执行内部静态对象的构造方法,如果有多个按定义的先后顺序执行:静态对象在构造的时候也是也先执行其内部的静态对象. 二.再调用父类的构造方法(父类还有父类的话,从最开始的基类开始调用),如果没有明显 ...

  3. 给软件增加注册功能 c#

    1.软件注册类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  4. 在windows中搭建php开发环境

    一.wampserver wampserver是一个安装集成包,包含了开发所需的apache,mysql,php,简单方便. 下载地址 http://www.xiazaiba.com/html/279 ...

  5. win8(64位)下memcache安装时报错“ failed to install service or service already installed” 与安装

    解决办法: 1.找到cmd.exe文件(c:\windows\system32\cmd.exe) 2.右键cmd.exe以管理员方式运行 3.把php_memcache.dll放到php的ext目录: ...

  6. 关于svcutil.exe

    添加环境变量 name NETFX4 value C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools Server.Pr ...

  7. 什么是Code Review

    Code Review 是一种通过复查代码提高代码质量的过程,在XP方法中占有极为重要的地位,也已经成为软件工程中一个不可缺少的环节. 本文通过对Code Review的一些概念和经验的探讨,就如何进 ...

  8. 对163k地方门户网站系统QQ互联功能修改

    163k地方门户网站QQ互联申请时遇到的问题: "禁止开发商强制用户重新注册或绑定其他帐号" 原因是用户登录完QQ还需要注册帐号或者绑定原有帐号 163k地方门户网站的QQ互联登录 ...

  9. s3c2416裸跑环境配置

    最近刚刚开始学习ARM-linux,上周买了块tq2416的板子,给的Linux资料太复杂太深奥不愿看,等不及想要把2416跑起来.于是到处找相关裸跑资料,可是用2416的人实在少,网上的资料更少,裸 ...

  10. Ueditor开发经验

    Ueditor是百度开发的一款免费使用的富文本编辑器. 先前就一直使用Ueditor,觉得功能挺多的,而且还给出了详细的文档,供二次开发. 但Ueditor已经出新的版本(和先前版本很不相同),网上很 ...