OC-谓词(NSPredicate)

OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。

下面直接上代码操作一下.

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) NSInteger age;

+ (instancetype)personWithName:(NSString *)name andAge:(NSInteger)age;
@end

创建一个Person类,里面有_name和_age两个属性.

Person.m

#import "Person.h"

@implementation Person

+ (instancetype)personWithName:(NSString *)name andAge:(NSInteger)age {
    
    Person *person = [[Person alloc]init];
    person.name = name;
    person.age = age;
    
    return person;
}


@end

main.h

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSArray *persons = [NSArray arrayWithObjects:
                            [Person personWithName:@"zhangsan" andAge:20],
                            [Person personWithName:@"lisi" andAge:25],
                            [Person personWithName:@"wangwu" andAge:22],
                            [Person personWithName:@"zhaoliu" andAge:30],
                            [Person personWithName:@"duqi" andAge:33],
                            [Person personWithName:@"zhengba" andAge:50],
                            [Person personWithName:@"liujiu" andAge:28],
                            [Person personWithName:@"zhangshi" andAge:79],
                            nil];
        

创建一个数组,元素是Person *对象.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d", 30];

NSArray *filterArray = [persons filteredArrayUsingPredicate:predicate];

创建一个谓词,也就是一个筛选条件,即年龄在30以下的,并且创建一个新的数组fileArray来接受符合条件的元素.

predicate = [NSPredicate predicateWithFormat:@"age < %d && name = %@", 30, @"zhangsan"];
filterArray = [persons filteredArrayUsingPredicate:predicate];
        
        for (Person *p in filterArray) {
            
            NSLog(@"pName : %@", p.name);
        }

当然也可以创建多个筛选条件的谓词

 predicate = [NSPredicate predicateWithFormat:@"self.name IN {'小白', '小黑'} || self.age IN {79, 28}"];

 filterArray = [persons filteredArrayUsingPredicate:predicate];
        for (Person *p in filterArray) {
            
            NSLog(@"pName : %@", p.name);
        }

使用IN符号

predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'A'"]; filterArray = [persons filteredArrayUsingPredicate:predicate];

        for (Person *p in filterArray) {

            NSLog(@"pName : %@", p.name);
        }

查找以字符''开头的

predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'A'"]; filterArray = [persons filteredArrayUsingPredicate:predicate];

        for (Person *p in filterArray) {

            NSLog(@"pName : %@", p.name);
        }

查找以字符''结尾的

predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
filterArray = [persons filteredArrayUsingPredicate:predicate];
        for (Person *p in filterArray) {
            
            NSLog(@"pName : %@", p.name);
        }

查找包含字符''的

//like 匹配任意多个字符  
//name中只要有s字符就满足条件  
predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
//?代表一个字符,下面的查询条件是:name中第二个字符是s的  
predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  

谓词的使用很方便,和我们当初在操作数据库的时候很像,但是它对我们进行过滤操作提供了很大的便捷。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,464评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,955评论 18 399
  • 《西游记》中,孙悟空、唐僧、猪八戒、沙和尚、白龙马这师徒五人只是一个人!孙悟空是人的心,唐僧是人的身,猪八戒是人的...
    合得阅读 1,794评论 0 0
  • 第四十三章 小猪说:“我被捉与你们被捉是截然不同,捉你们是为了剪毛或挤奶,捉我却是为了吃我的肉。意思是对待财产与对...
    维扬之水阅读 1,917评论 0 3
  • 空气布满被肺而污染的烟草味, 眼圈涨红,坚韧的不肯合闭, 黑夜的黑暗躲进眼底。 脖颈上摆动的十字架, 看着主人狰狞...
    罡罡不见甚是想念阅读 767评论 0 0