謂詞的使用 -ios

來源:互聯網
上載者:User

標籤:des   blog   使用   os   io   for   ar   div   

#import <Foundation/Foundation.h>@interface Person : NSObject<NSCopying>@property(nonatomic,copy) NSString *name;@property(nonatomic,retain) NSNumber *age;-(void) setNewName:(NSString *) name;@end

 

#import "Person.h"@implementation Person:NSObject- (id)copyWithZone:(NSZone *)zone{    Person *person=[[[self class] allocWithZone:zone] init];    person.name=_name;    person.age=_age;    return person;}-(void) setNewName:(NSString *) name{    self.name=name;}-(NSString *)description{    NSString *description=[NSString stringWithFormat:@"年齡:%@;名字:%@",_age,_name];    return description;}@end

 

//謂詞用法 NSPredicate        NSMutableArray *array=[NSMutableArray array];        for (int i=0; i<10; i++) {            Person *person=[[Person alloc]init];            [person setAge:@(20+i)];            person.name=[NSString stringWithFormat:@"jage-%d",i];            [array addObject:person];        }        //大,小,等;等運算過濾        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"age<%d",25];        NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"age<23",23];        NSPredicate *predicate3=[NSPredicate predicateWithFormat:@"age<27 and age>25"];        NSPredicate *predicate4=[NSPredicate predicateWithFormat:@"age<27 && age>25"];        NSPredicate *predicate5=[NSPredicate predicateWithFormat:@"age<23 || age>26"];        NSPredicate *predicate6=[NSPredicate predicateWithFormat:@"name=‘jage-3‘"];        //在某個裡面        NSPredicate *predicate7=[NSPredicate predicateWithFormat:@"name in {‘jage-1‘,‘jage-5‘}"];        NSArray *[email protected][@"jage-1",@"jage-4",@"jage-3"];        NSPredicate *predicate8=[NSPredicate predicateWithFormat:@"name in %@",inArray];        //已什麼開頭,注意加單引號        NSPredicate *predicate9=[NSPredicate predicateWithFormat:@"name BEGINSWITH ‘jage‘"];        //已什麼結尾,注意加單引號        NSPredicate *predicate10=[NSPredicate predicateWithFormat:@"name ENDSWITH ‘-9‘"];        //包含,注意加單引號        NSPredicate *predicate11=[NSPredicate predicateWithFormat:@"name CONTAINS ‘-3‘"];        //like,注意加單引號        NSPredicate *predicate12=[NSPredicate predicateWithFormat:@"name LIKE ‘jage-?‘"];        NSPredicate *predicate13=[NSPredicate predicateWithFormat:@"name LIKE ‘*-2‘"];                for (Person *person in array) {            if([predicate6 evaluateWithObject:person]){//逐個對象判斷                NSLog(@"%@",person);            }        }        //對數組過濾       NSArray *filterArray=[array filteredArrayUsingPredicate:predicate13];        NSLog(@"%@",filterArray);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.