NSArray 查询数组中的对象

2024-04-12 08:58
文章标签 数组 查询 对象 nsarray

本文主要是介绍NSArray 查询数组中的对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.NSString 对象

NSArray  *array =@["123", @"234" , @"345"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", "2"];

NSArray *filterdArray = [array filterdArrayUsingPredicate:predicate];

<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="typ">NSLog</span><span class="pun">(@</span><span class="str">"%@"</span><span class="pun">,</span><span class="pln"> filterdArray </span><span class="pun">);</span></strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="com">//output : @"123", "234"</span></strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="pln">
</span></strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="lit">2.</span><span class="pun">含有属性的对象</span></strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="pln">
</span></strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong></strong></span>
<code class="prettyprint"><span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="typ"></span></strong></span></code><pre name="code" class="lang-c prettyprint prettyprinted"><code class="prettyprint"><span class="lit"><span class="lit">@interface</span></span><span class="pln"><span class="pln"> </span><span class="typ">Person</span></span><span class="pun"><span class="pun">:</span></span><span class="pln"><span class="pln"> </span></span><span class="typ"><span class="typ">NSObject</span></span><span class="pln"><span class="pln"> </span></span></code>
<code class="prettyprint"><span class="pun"><span class="pun">{</span></span><span class="pln"><span class="pln"></span></span><span class="typ"><span class="typ">NSString</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span><span class="pln">_</span></span><span class="pln"><span class="pln">name</span></span><span class="pun"><span class="pun">;</span></span><span class="pln"><span class="pln"></span><span class="typ">NSString</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span></span><span class="pln"><span class="pln">_telephone</span></span><span class="pun"><span class="pun">;</span></span></code>
<code class="prettyprint"><span class="pln"><span class="pln">    </span><span class="typ">NSInteger</span><span class="pln"> _id</span><span class="pun">;</span><span class="pln">
</span></span><span class="pun"><span class="pun">}</span></span><span class="pln"><span class="pln"></span></span><span class="lit"><span class="lit">@property</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">nonatomic</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> copy</span></span><span class="pun"><span class="pun">)</span></span><span class="pln"><span class="pln"> </span></span><span class="typ"><span class="typ">NSString</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span><span class="pln">name</span><span class="pun">;</span><span class="pln">
</span></span><span class="lit"><span class="lit">@property</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">nonatomic</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> </span></span></code><code class="prettyprint"><span class="pln">copy</span></code><code class="prettyprint"><span class="pun"><span class="pun">)</span></span><span class="pln"><span class="pln"> </span></span></code><span class="typ"><span class="typ">NSString</span></span><span class="pln"><span class="pln"> </span><span class="pun">*</span><span class="pln">telephone</span><span class="pun">;</span></span>
<span class="pln"></span><pre name="code" class="lang-c prettyprint prettyprinted"><code class="prettyprint"><span class="lit"><span class="lit">@property</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">(</span></span><span class="pln"><span class="pln">nonatomic</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> </span></span></code><code class="prettyprint"><span class="pln">assign</span></code><code class="prettyprint"><span class="pun"><span class="pun">)</span><span class="pln"> </span></span></code><span style="background-color:rgb(255,255,255)"><span class="typ">NSInteger</span><span class="pln"> id</span><span class="pun">;</span></span><span class="pln"></span>

 
<code class="prettyprint"><span class="pun"><span class="lit">@end</span></span><span class="pln"></span></code>
 
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="com">//</span></strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><strong><span class="lit">1</span><span class="pun">).</span></strong></span>
<pre name="code" class="lang-c prettyprint prettyprinted"><code class="prettyprint"><span style="font-family:KaiTi_GB2312; font-size:18px; background-color:rgb(255,255,255)"><strong><span class="typ"><span class="typ">NSPredicate</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span></span><span class="pln"><span class="pln">predicate </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">[</span></span><span class="typ"><span class="typ">NSPredicate</span></span><span class="pln"><span class="pln"> predicateWithFormat</span></span><span class="pun"><span class="pun">:@</span></span><span class="str"><span class="str">"</span><code class="prettyprint"><span class="str">name </span></code><span class="str"><span class="str">== %@"</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> </span><span class="str">"Ansel"</span></span><span class="pun"><span class="pun">];</span></span><span class="pln"><span class="pln"> </span></span><span class="pln">
</span></span><span class="typ"><span class="typ">NSArray</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span></span><span class="pln"><span class="pln">filteredArray </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">[</span></span><span class="pln"><span class="pln">array filteredArrayUsingPredicate</span></span><span class="pun"><span class="pun">:</span></span><span class="pln"><span class="pln">predicate</span></span><span class="pun"><span class="pun">];</span></span></strong></span></code>
<code class="prettyprint"><span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><span class="pun"><strong></strong></span></span></code><pre name="code" class="lang-c prettyprint prettyprinted"><span style="font-size:18px;background-color:rgb(255,255,255)"><span class="pln">
</span></span>
<span style="font-size:18px;background-color:rgb(255,255,255)"><span class="lit">2</span><span class="pun">).</span></span>
 
<code class="prettyprint"><span style="font-family:KaiTi_GB2312;font-size:18px;background-color:rgb(255,255,255)"><span class="pun"><strong></strong></span></span></code><pre name="code" class="lang-c prettyprint prettyprinted"><code class="prettyprint"><span style="font-size:18px; background-color:rgb(255,255,255)"><span class="typ"><span class="typ">NSPredicate</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span></span><span class="pln"><span class="pln">predicate </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">[</span></span><span class="typ"><span class="typ">NSPredicate</span></span><span class="pln"><span class="pln"> predicateWithFormat</span></span><span class="pun"><span class="pun">:@</span></span><span class="str"><span class="str">"</span><code class="prettyprint"><span class="str">id</span></code><span class="str"><span class="str">== %@"</span></span><span class="pun"><span class="pun">,</span></span><span class="pln"><span class="pln"> </span><span class="pun">@</span><span class="lit">13</span></span><span class="pun"><span class="pun">];</span></span><span class="pln"><span class="pln"> </span><span class="com">//predicate只能是对象</span></span><span class="pln">
</span></span><span class="typ"><span class="typ">NSArray</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">*</span></span><span class="pln"><span class="pln">filteredArray </span></span><span class="pun"><span class="pun">=</span></span><span class="pln"><span class="pln"> </span></span><span class="pun"><span class="pun">[</span></span><span class="pln"><span class="pln">array filteredArrayUsingPredicate</span></span><span class="pun"><span class="pun">:</span></span><span class="pln"><span class="pln">predicate</span></span><span class="pun"><span class="pun">];</span></span></span></code>

 

                                    

这篇关于NSArray 查询数组中的对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896677

相关文章

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

浅谈mysql的sql_mode可能会限制你的查询

《浅谈mysql的sql_mode可能会限制你的查询》本文主要介绍了浅谈mysql的sql_mode可能会限制你的查询,这个问题主要说明的是,我们写的sql查询语句违背了聚合函数groupby的规则... 目录场景:问题描述原因分析:解决方案:第一种:修改后,只有当前生效,若是mysql服务重启,就会失效;

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固 通俗易懂版)

《MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固通俗易懂版)》本文主要讲解了MySQL中的多表查询,包括子查询、笛卡尔积、自连接、多表查询的实现方法以及多列子查询等,通过实际例子和操... 目录复合查询1. 回顾查询基本操作group by 分组having1. 显示部门号为10的部门名,员

Java实现将byte[]转换为File对象

《Java实现将byte[]转换为File对象》这篇文章将通过一个简单的例子为大家演示Java如何实现byte[]转换为File对象,并将其上传到外部服务器,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言1. 问题背景2. 环境准备3. 实现步骤3.1 从 URL 获取图片字节数据3.2 将字节数组

mysql关联查询速度慢的问题及解决

《mysql关联查询速度慢的问题及解决》:本文主要介绍mysql关联查询速度慢的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql关联查询速度慢1. 记录原因1.1 在一次线上的服务中1.2 最终发现2. 解决方案3. 具体操作总结mysql

Javascript访问Promise对象返回值的操作方法

《Javascript访问Promise对象返回值的操作方法》这篇文章介绍了如何在JavaScript中使用Promise对象来处理异步操作,通过使用fetch()方法和Promise对象,我们可以从... 目录在Javascript中,什么是Promise1- then() 链式操作2- 在之后的代码中使