iOS 查询数组中的对象

通常情况下,根据一个条件在数组中查询出匹配的对象的方式有如下几种:

(1)遍历数组,然后将每一个Item和这个条件进行比对,过滤出匹配的对象
显然这个效率比较低

(2)iOS提供另一个效率较高的查询方法,谓词NSPredicate,使用方法如下
C代码 收藏代码
1.NSString 对象

NSArray *array [email protected]["123", @"234" , @"345"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", "2"];
NSArray *filterdArray = [array filterdArrayUsingPredicate:predicate];
NSLog(@"%@", filterdArray );
//output : @"123", "234"

2.含有属性的对象

@interface Person: NSObject
{
NSString *_name;
NSString *_telephone;
NSInteger _id;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *telephone;
@property (nonatomic, assign) NSInteger id;

@end
//

1).等于查询
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", "Ansel"];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];

2).模糊查询
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"A"]; //predicate只能是对象
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
更详细的用法,请见这个帖子:
http://www.cnblogs.com/thefeelingofsimple/archive/2013/01/31/2886915.html

时间: 2024-08-25 17:25:01

iOS 查询数组中的对象的相关文章

[示例]NSPredicate基础-查询数组中负荷条件的子集

代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { /* 简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 定义(最常用到的方法): NSPredicate这个类有点类似于数据库中的查询,是用于在一批内容中查询符合条件的子集,中文翻译成“谓词”.这个翻译实在让我感觉很别扭,虽然

20150310-删除数组中原有对象

一.删除数组中原有对象 [self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

用 inArray 查找数组中的对象

inArray 是jquery 用来数组查重的函数,很多时候数组中含有对象,直接查找是找不到的,必须转换成 JSON字符串来进行查找. $map 在这里简化了很多代码. var arr = [ {"id" : "1", "description" : "one"}, {"id" : "2", "description" : "two"}, {&qu

关于iOS去除数组中重复数据的几种方法

关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方法 可以将NSArray中的元素存入一个字典,然后利用AllKeys或者AllValues取得字典的所有键或值,这些键或值都是去重的.代码: NSArray *dataArray = @[@"2014-04-01",@"2014-04-02",@"2014-04-

对数组中的对象进行升序以及降序的排序方法(其中对象类型是系统类型).

[cpp] view plaincopyprint? <span style="font-size:32px;">NSArray *arr = [[NSArray alloc] initWithObjects:@"aa",@"bb",@"cc",@"dd",@"ee",@"ff", nil]; //1.对数组进行升序排序 //sortedArrayUsin

in_array 查询数组中是否存在某个值

(PHP 4, PHP 5) in_array — 检查数组中是否存在某个值 说明 bool in_array ( mixed $needle , array $haystack [, bool $strict ] ) 在 haystack 中搜索 needle,如果找到则返回 TRUE,否则返回 FALSE. 如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和haystack 中的相同. Note: 如果 needle 是字符串,

如何高效的查询数组中是否包含某个值

一.有四种方式查询数组中是否包含某个值 1.使用List public static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue); } 2.使用Set public static boolean useSet(String[] arr, String targetValue) { Set<String> set = new HashSet&

C语言--查询数组中出现次数最多的元素

查询数组中出现次数最多的元素 #include <stdio.h> #include <malloc.h> #include <stdlib.h> int max_count_num(int * arr, int len); int main() { int arr[5] = {1, 1, 1, 3, 1 }; max_count_num(arr, 5); return 0; } int max_count_num(int * arr, int len) { int i

使用JavaScript从数组中删除对象

如何从数组中删除对象? 我希望从someArray删除包含名称Kristian的对象. 例如: someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}]; 我要实现: someArray = [{name:"John", lines:"1,19,26,96"}]; #1楼 在数组上