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

相关文章

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

java中VO PO DTO POJO BO DO对象的应用场景及使用方式

《java中VOPODTOPOJOBODO对象的应用场景及使用方式》文章介绍了Java开发中常用的几种对象类型及其应用场景,包括VO、PO、DTO、POJO、BO和DO等,并通过示例说明了它... 目录Java中VO PO DTO POJO BO DO对象的应用VO (View Object) - 视图对象

Redis KEYS查询大批量数据替代方案

《RedisKEYS查询大批量数据替代方案》在使用Redis时,KEYS命令虽然简单直接,但其全表扫描的特性在处理大规模数据时会导致性能问题,甚至可能阻塞Redis服务,本文将介绍SCAN命令、有序... 目录前言KEYS命令问题背景替代方案1.使用 SCAN 命令2. 使用有序集合(Sorted Set)

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

Java将时间戳转换为Date对象的方法小结

《Java将时间戳转换为Date对象的方法小结》在Java编程中,处理日期和时间是一个常见需求,特别是在处理网络通信或者数据库操作时,本文主要为大家整理了Java中将时间戳转换为Date对象的方法... 目录1. 理解时间戳2. Date 类的构造函数3. 转换示例4. 处理可能的异常5. 考虑时区问题6.

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

PostgreSQL如何查询表结构和索引信息

《PostgreSQL如何查询表结构和索引信息》文章介绍了在PostgreSQL中查询表结构和索引信息的几种方法,包括使用`d`元命令、系统数据字典查询以及使用可视化工具DBeaver... 目录前言使用\d元命令查看表字段信息和索引信息通过系统数据字典查询表结构通过系统数据字典查询索引信息查询所有的表名可

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来