Use of predicates-ios

Source: Internet
Author: User

#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:%@", _age,_name];    return description;} @end

predicate usage 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];        }//large, small, etc, etc arithmetic filtering 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 '"];        Inside a certain 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];        What has started, note plus single quotes nspredicate *predicate9=[nspredicate predicatewithformat:@ "name Beginswith ' Jage '"];        What has ended, note the addition of single quotes Nspredicate *predicate10=[nspredicate predicatewithformat:@ "Name ENDSWITH '-9 '"];        Included, note plus single quotes nspredicate *predicate11=[nspredicate predicatewithformat:@ "Name CONTAINS '-3 '"];        Like, note the extra single quote nspredicate *predicate12=[nspredicate predicatewithformat:@ "name like ' jage-?"];                Nspredicate *predicate13=[nspredicate predicatewithformat:@ "name like ' *-2 '"]; For (person *person in array) {if ([Predicate6 Evaluatewithobject:person]) {//Object-by-case NSLog (@ "%@",            person);        }}//log Group filter Nsarray *filterarray=[array FILTEREDARRAYUSINGPREDICATE:PREDICATE13]; NSLog (@ "%@", Filterarray);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.