"Turn" IOS learning nspredicate fuzzy, accurate, query

Source: Internet
Author: User

From: http://blog.csdn.net/lianbaixue/article/details/10579117 brief description: The nspredicate in the cocoa Framework is used for querying, both in principle and in usage similar to where in SQL, The function is equivalent to the filter fetch of the database.

Definitions (most commonly used methods):

Nspredicate *ca = [nspredicate predicatewithformat: (NSString *), ...];  
Format:
(1) comparison operator >,<,==,>=,<=,!=
Available for numeric and string
Example: @ "number > 100"

(2) Scope operator: in, between
Example: @ "number between {1,5}"
@ "address in {' Shanghai ', ' Beijing '}"

(3) string itself: Self
Example: @ "self = = ' APPLE '"

(4) String correlation: Beginswith, ENDSWITH, CONTAINS
Example: @ "name CONTAIN[CD] ' ang '"//contains a string
@ "name Beginswith[c] ' sh '"//start with a string
@ "name Endswith[d] ' ang '"//End with a string
Note: [c] case insensitive [d] does not distinguish between pronounced symbols and no accent marks [CD] are neither case sensitive nor pronounced.

(5) wildcard character: like
Example: @ "name LIKE[CD] ' *er* '"//* represents a wildcard, like also accepts [CD].
@ "name LIKE[CD] '??? Er* ' "(6) Regular expression: MATCHES

Example: NSString *regex = @ "^a.+e$"; Start with a, E end
@ "Name MATCHES%@", regex

Practical application:
(1) Filter the Nsarray

Nsarray *array = [[Nsarray alloc]initwithobjects:@ "Beijing", @ "Shanghai", @ "Guangzou", @ "Wuhan", Nil];    NSString *string = @ "Ang";    Nspredicate *pred = [Nspredicate predicatewithformat:@ "Self CONTAINS%@", string];    
(2) Determine if the first letter of the string is a letter:
NSString *regex = @ "[a-za-z]+";    Nspredicate *predicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];        if ([predicate evaluatewithobject:astring]) {    }  
(3) string substitution:
nserror* error = NULL;    nsregularexpression* regex = [Nsregularexpression regularexpressionwithpattern:@ "(encoding=\") [^\ "]+ (\") "                                                                                options:0                                                                                error:&error];    nsstring* sample = @ "<xml encoding=\" abc\ "></xml><xml encoding=\" def\ "></xml><xml Encoding=\ "ttt\" ></xml> ";    NSLog (@ "start:%@", sample);    nsstring* result = [regex stringbyreplacingmatchesinstring:sample                                                          options:0                                                           range:nsmakerange (0, sample.length)                                                          withtemplate:@ "$1utf-8$2"];    NSLog (@ "result:%@", Result);
(4) intercept the string as follows:
//assemble a string that needs to parse the URL inside nsstring *[email protected] "<meta/><        link/><title>1q84 book1</title>
  (5) Determine cell phone number, phone number function  
Regular Judge mobile phone number address format-(BOOL) Ismobilenumber: (NSString *) mobilenum {/** * mobile number * Mobile: 134[0-8],135,136,          137,138,139,150,151,157,158,159,182,187,188 * Unicom: 130,131,132,152,155,156,185,186 * Telecom: 133,1349,153,180,189         */NSString * MOBILE = @ "^1 (3[0-9]|5[0-35-9]|8[025-9]) \\d{8}$"; /** 10 * Mobile: China Mobile 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,1 * */NSString * CM = @ "^1 (34[0-8]| (         3[5-9]|5[017-9]|8[278] \\d) \\d{7}$ ";         /** 15 * Unicom: China Unicom 16 * 130,131,132,152,155,156,185,186 17 * *         NSString * CU = @ "^1 (3[0-2]|5[256]|8[56]) \\d{8}$"; /** 20 * China Telecom: Chinese Telecom * 133,1349,153,180,189 * * * Nsstri         ng * CT = @ "^1 ((33|53|8[09]) [0-9]|349) \\d{7}$"; /** 25 * Mainland China Landline and PHS 26 * Area code: 010,020,021,022,023,024,025,027,028,029 27 * Number: seven-bit or eight-bit * *//NSString * PHS = @ "^0 (10|2[0-5789)             |\\D{3}) \\d{7,8}$ ";       Nspredicate *regextestmobile = [Nspredicate predicatewithformat:@ "Self MATCHES%@", MOBILE];       Nspredicate *REGEXTESTCM = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CM];       Nspredicate *REGEXTESTCU = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CU];            Nspredicate *REGEXTESTCT = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CT]; if ([regextestmobile evaluatewithobject:mobilenum] = = YES) | | ([regextestcm evaluatewithobject:mobilenum] = = YES) | | ([regextestct evaluatewithobject:mobilenum] = = YES) | |      ([regextestcu evaluatewithobject:mobilenum] = = YES))          {if ([regextestcm evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Mobile");          } else if ([regextestct evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Telecom"); } ElSe if ([regextestcu evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Unicom");          } else {NSLog (@ "Unknow");      } return YES;      } else {return NO;   }  }
(6) Email verification, phone number verification:
is a valid regular expression    + (BOOL) isvalidateregularexpression: (NSString *) strdestination byexpression: (NSString *) strexpression    {       nspredicate *predicate = [nspredicatepredicatewithformat:@ "Self MATCHES%@", strexpression];         return [predicate evaluatewithobject:strdestination];    }    Verify email  + (BOOL) Isvalidateemail: (NSString *) Email {       NSString *strregex = @ "[A-z0-9a-z._%+-][email protected ][a-za-z0-9.-]+\\. [A-za-z] {1,5} ";       BOOL RT = [Commontools isvalidateregularexpression:email Byexpression:strregex];       return RT;    }    Verify phone  + (BOOL) Isvalidatetelnumber: (NSString *) Number {       NSString *strregex = @ "[0-9]{1,20}";       BOOL RT = [Commontools isvalidateregularexpression:number Byexpression:strregex];       return RT;    }  
(7) NSDate to filter
Date within 10 days:  nsdate *enddate = [[NSDate date] retain];  Nstimeinterval timeinterval= [EndDate timeintervalsincereferencedate];  TimeInterval-=3600*24*10;  NSDate *begindate = [[NSDate datewithtimeintervalsincereferencedate:timeinterval] retain];  Filter CoreData (assuming fetchrequest)  nspredicate *predicate_date =  [nspredicate predicatewithformat:@ "date >=%@ and date <=%@ ", begindate,enddate];        [Fetchrequest setpredicate:predicate_date];  Release the object of Retained  [EndDate release];  

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.