IOS 8: "Go" nspredicate notes

Source: Internet
Author: User

Source Address: http://fann.im/blog/2012/09/07/nspredicate-notes/

In Core data, you can assign a predicate to Nsfetchrequest to filter the data to make it easier to find, such as:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"id == %@", 123];

Nspredicate's filtering query rules apply not only to Core Data, but also to string filtering. Like what:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", @"hello"];BOOL b = [predicate evaluateWithObject:@"hello world"]; // YES

contains beginswith endswith The like syntax matches for string support isand/or/not/in

nspredicate *predicate1 = [nspredicate predicatewithformat:@ "self BEGINSWITH%@" @ "Hello" ];b = [predicate1  Evaluatewithobject:@ "Hello World" //yesbool n = [ Predicate1 evaluatewithobject:@ "Nohello World"  ; //NO                

likeMatches, supports * any character (none), and ? has only one character:

Nspredicate*Like=[NspredicatePredicatewithformat:@ "Self like%@",@ "*like?" ; nslog (@ "%d" [ Like evaluatewithobject:@ "alike" ); //0-nonslog (@ "%d"  [like evaluatewithobject:@ " 000liked "//1-yesnslog (@ "%d"  [like evaluatewithobject:@ " Likes "]); //1-yes               

matchesRegular match:

Nspredicate*Match=[NspredicatePredicatewithformat:@ "Self MATCHES"\\\\D+[a-z] ' "];NSLog(@ "%d",[MatchEvaluatewithobject:@ "0A"); //nonslog (@ "%d"  [match evaluatewithobject:@ "0a "]); //yesnslog (@ "%d"  [match evaluatewithobject:@ " 000000ab "//nonslog (@ "%d"  [match evaluatewithobject:@ " 000000c "//YES                

Nspredicate can be used together, which is also the most convenient place, such as the following example:

The string starts with CH, is longer than 3 and is less than 20 characters, contains at least one number, does not contain broken, and does not contain spaces.

Nspredicate*One=[NspredicatePredicatewithformat:@ "Self beginswith ' CH '"];Nspredicate*Both=[NspredicatePredicatewithformat:@ "Self.length > 3 and Self.length < 20"];Nspredicate*Three=[NspredicatePredicatewithformat:@ "Self MATCHES". *\\\\D.* ' "];Nspredicate*Four=[NspredicatePredicatewithformat:@ "Not (self CONTAINS ' broken ') and not (self CONTAINS ')"];Nsarray*Array=[NsarrayArraywithobjects:One,Both,Three,Four,Nil; nspredicate *predicate = [nscompoundpredicate andpredicatewithsubpredicates: Array; nslog (@ "%d" [ predicate evaluatewithobject:@ "Ch998broken" ); //nonslog (@ "%d"  [predicate evaluatewithobject: @ "CH998" //yes                

@"attributeName == %@": The value of the key AttributeName is the same as the value of the object (NSDate, NSNumber, Nsdecimalnumber, or NSString ). Completely equal judgment.

@"%K == %@": The value of the key%K is the same as the value of the object%@. The value corresponding to the key is equal to the given value.

@"name IN $NAME_LIST": The value of the key name is in the variable $NAME _list. @"‘name‘ IN $NAME_LIST": The constant value ' name ' (note the quotes around the string) is in the variable $NAME _list. Determine whether the value in the specified list, the former judge is ‘name‘ is the name string to judge.

Resources:

    • Nspredicates for fun and Profit, very good nspredicate introduction
    • Apple document predicate Programming Guide.

IOS 8: "Go" nspredicate notes

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.