• OC中给我们提供的一个技术:谓词(NSPredicate).note


    OC中给我们提供的一个技术:谓词(NSPredicate)
    OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。


    下面来看一下具体的例子吧:
    Person.h
    objc]  view plaincopy 
    1. //  
    2. //  Person.h  
    3. //  46_NSPredicate  
    4. //  
    5. //  Created by jiangwei on 14-10-18.  
    6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10.   
    11. @interface Person : NSObject  
    12.   
    13. @property NSString *name;  
    14. @property NSInteger age;  
    15.   
    16. + (id)personWithName:(NSString *)name andAge:(NSInteger)age;  
    17.   
    18. @end 
    Person.m
    [objc]  view plaincopy 
    1. //  
    2. //  Person.m  
    3. //  46_NSPredicate  
    4. //  
    5. //  Created by jiangwei on 14-10-18.  
    6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
    7. //  
    8.   
    9. #import "Person.h"  
    10.   
    11. @implementation Person  
    12.   
    13. + (id)personWithName:(NSString *)name andAge:(NSInteger)age{  
    14.     Person *person = [[Person alloc] init];  
    15.     person.name = name;  
    16.     person.age = age;  
    17.     return person;  
    18. }  
    19.   
    20. - (NSString *)description{  
    21.     NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];  
    22.     return s;  
    23. }  
    24.   
    25. @end 
    我们在Person类中定义属性,还有一个产生对象的类方法,同时重写了description方法,用于打印结果
     
    测试方法
    main.m
    [objc]  view plaincopy 
    1. //  
    2. //  main.m  
    3. //  46_NSPredicate  
    4. //  
    5. //  Created by jiangwei on 14-10-18.  
    6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10. #import "Person.h"  
    11.   
    12. //谓词,指定过滤器的条件,将符合条件的对象保留下来  
    13. //一般用谓词过滤数组中指定的元素  
    14. int main(int argc, const charchar * argv[]) {  
    15.     @autoreleasepool {  
    16.          
    17.         NSArray *persons = [NSArray arrayWithObjects:  
    18.                             [Person personWithName:@"mac" andAge:20],  
    19.                             [Person personWithName:@"1" andAge:30],  
    20.                             [Person personWithName:@"2" andAge:40],  
    21.                             [Person personWithName:@"3" andAge:50],  
    22.                             [Person personWithName:@"4" andAge:60],  
    23.                             [Person personWithName:@"5" andAge:70],  
    24.                             [Person personWithName:@"6" andAge:20],  
    25.                             [Person personWithName:@"7" andAge:40],  
    26.                             [Person personWithName:@"8" andAge:60],  
    27.                             [Person personWithName:@"9" andAge:40],  
    28.                             [Person personWithName:@"0" andAge:80],  
    29.                             [Person personWithName:@"10" andAge:90],  
    30.                             [Person personWithName:@"1" andAge:20]];  
    31.           
    32.         //年龄小于30  
    33.         //定义谓词对象,谓词对象中包含了过滤条件  
    34.         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
    35.         //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
    36.         NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
    37.         NSLog(@"filterArray=%@",array);  
    38.           
    39.         //查询name=1的并且age大于40  
    40.         predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
    41.         array = [persons filteredArrayUsingPredicate:predicate];  
    42.         NSLog(@"filterArray=%@",array);  
    43.           
    44.         //in(包含)  
    45.         predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  
    46.           
    47.         //name以a开头的  
    48.         predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
    49.         //name以ba结尾的  
    50.         predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
    51.           
    52.         //name中包含字符a的  
    53.         predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  
    54.           
    55.         //like 匹配任意多个字符  
    56.         //name中只要有s字符就满足条件  
    57.         predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
    58.         //?代表一个字符,下面的查询条件是:name中第二个字符是s的  
    59.         predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  
    60.           
    61.           
    62.           
    63.     }  
    64.     return 0;  
    65. } 
    首先我们看到,我们初始化了一定大小的数组。
    然后我们就可以使用NSPredicate类进行过滤操作了
     
    1、查询数组中年龄小于30的对象
    [objc]  view plaincopy 
    1. //年龄小于30  
    2. //定义谓词对象,谓词对象中包含了过滤条件  
    3. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
    4. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
    5. NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
    6. NSLog(@"filterArray=%@",array);  
    首先创立一个过滤条件:
    [objc]  view plaincopy 
    1. //定义谓词对象,谓词对象中包含了过滤条件  
    2. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
    这里面操作很简单的:@"age<%d",这个age是Person的属性名,%d相当于占位符,然后后面用参数替换即可
    然后进行过滤操作,返回一个过滤之后的数组对象
    [objc]  view plaincopy 
    1. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
    2. NSArray *array = [persons filteredArrayUsingPredicate:predicate]; 
    2、查询name=1并且age大于40的集合
    [objc]  view plaincopy 
    1. //查询name=1的并且age大于40  
    2. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
    3. array = [persons filteredArrayUsingPredicate:predicate];  
    4. NSLog(@"filterArray=%@",array);  
    当然我们也可以使用&&进行多条件过滤
     
    3、包含语句的使用
    [objc]  view plaincopy 
    1. //in(包含)  
    2. predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
     
    4、指定字符开头和指定字符结尾,是否包含指定字符
    [objc]  view plaincopy 
    1. //name以a开头的  
    2. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
    3. //name以ba结尾的  
    4. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
    5.   
    6. //name中包含字符a的  
    7. predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  
     
    5like进行匹配多个字符
    [objc]  view plaincopy 
    1. //like 匹配任意多个字符  
    2. //name中只要有s字符就满足条件  
    3. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
    4. //?代表一个字符,下面的查询条件是:name中第二个字符是s的  
    5. predicate = [NSPredicate predicateWithFormat:@"name like '?s'"]; 
    总结
    这一篇就介绍了OC中常用的技术:谓词的使用,他用起来很方便的,而且也没什么难度,和我们当初在操作数据库的时候很想,但是他对我们进行过滤操作提供了很大的便捷。
     
  • 相关阅读:
    国内医保控费公司简单比较
    程序员生存定律--管理向左,技术向右
    程序员生存定律--细论软件这个行当的根本特征
    程序员生存定律--细论影响人生成绩的四个要素(2)
    程序员生存定律--细论影响人生成绩的四个要素(1)
    程序员生存定律--定律的概要
    程序员生存定律--交换是职场里一切的根本
    程序员生存定律--目录
    程序员生存定律--那个是你的人生出口
    程序员生存定律--程序人生的出口
  • 原文地址:https://www.cnblogs.com/GhostKZShadow/p/5105470.html
Copyright © 2020-2023  润新知