iOS development, verb (nspredicate) usage: (i) basic usage

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="white-space:pre"></span></p></p><pre name="code" class="objc"><pre name="code" class="objc"><span style= "white-space:pre;" ></span> in iOS development, the system provides nspredicate this class for us to do some matching, filtering operations, very Convenient. In the absence of this class, we need to write a code one by one comparison when we want to get some specific elements of two arrays, but with this class, only three or four lines of code are Required.</pre></pre><p><p></p></p><p><p><span style="white-space:pre"></span>To demonstrate, define a person class first</p></p><p><p><span style="white-space:pre"></span>. h file</p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import <Foundation/Foundation.h> @interface person:nsobject@property (nonatomic, copy) nsstring *name;@ Property (nonatomic, assign) nsinteger age;-(instancetype) initwithname: (nsstring *) name Age : (nsinteger) age;+ ( Person *) personwithname: (nsstring *) nsme age : (nsinteger) age; @end</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>. m file<p><p></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import "Person.h" @implementation Person-(instancetype) initwithname: (nsstring *) name age: (nsinteger) age { if ( Self = [super init]) { _name = name; _age = age; } Return self;} + (person *) personwithname: (nsstring *) nsme age: (nsinteger) age {person *person = [[person alloc] initwithname:nsme A ge:age]; Return person;} -(nsstring *) Description { return [nsstring stringwithformat:@ "name =%@, age =%ld", _name, _age];} @end</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>I overridden the description method in the implementation file to print out the value of the Object's properties when the filter results are Printed.<p><p></p></p><p><p><span style="white-space:pre"></span>The person is then introduced into the main file (for demonstration purposes, No cocoa is used), and 10 different object objects are created and placed in the Array.</p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">Create 10 person objects and add the array nsmutablearray *muarray = [nsmutablearray array]; for (int i = 0; i <10; i++) {person *person = [person personwithname:[nsstring stringwithformat:@ ' zhang%d ', i] age: i]; [muarray addobject:person]; } NSLog (@ "muarray =%@", muarray);</pre></pre><p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>ok, ready to finish the work, the protagonist is Shining.</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>Nspredicate is used mainly in two steps: the first step, the definition of predicate sentence, the second step, according to the requirements, choose different methods to implement the predicate sentence.</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>first, we define a predicate sentence, which, when defining a predicate sentence, introduces the syntax of the Predicate.</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>1) comparison Operators</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">/** comparison operator *;: greater than * <: less than * >=: greater than or equal to * <=: less than or equal to * =,==: equals * !=,<>: Not equal to * Between: the expression on the left equals the value of the expression on the right or between them. On the right is a sequence of two values that specify the upper and lower bounds (the sequence of the specified order). For example, 1 between {0, 33}, or $input between {$LOWER, $UPPER}. **/ ///example: Age attribute is more than 3 years old, note: key path cannot be enclosed in single quotation marks, otherwise error nspredicate *predicate = [nspredicate predicatewithformat:@ ' Age > 3 "];</pre></pre><span style="white-space:pre"><span style="white-space:pre"> </span></span>2) logical operators<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc"> /** logical operator * and/&&: with * or/| |: or * not/!: non- **/ //example: Older than three years old or named "zhang1", Note: the value of the string needs to be enclosed in single quotation marks, otherwise it will be an error message: this class was not key value coding-compliant for the key zhang1. Nspredicate *predicate = [nspredicate predicatewithformat:@ "age > 3 | | name = ' ZHANG1 '"];</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>3) Relational operators<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">/** Relationship Operation * any,some: Specifies any element in the following Expression. For example, any children.age < 18. * All: specifies all elements in the following Expression. For example, all Children.age < 18. * none: specifies an element that is not in the following Expression. For example, NONE Children.age < 18. It is logically equal to not (any ...). * In: equals the in operation of sql, the left expression must appear in the collection specified on the Right. For example, name in {' Ben ', ' Melissa ', ' Nick '}. **/ //example: in keyword: the Left keyword must contain the elements of the right collection nspredicate *predicate = [nspredicate predicatewithformat:@ "name In {' zhang1 ', ' Zhang4 '} "];</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>4) Array Manipulation<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">/** array Operation * array[index]: Specifies the element at a particular index in the Array. * array[first]: Specifies the first element in the Array. * array[last]: Specifies the last element in the Array. * array[size]: Specifies the size of the ARRAY. **/</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>The above is the definition of predicates, as we can see, I am mainly using predicatewithformat: This class method is Defined.<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>The second step: the execution of the predicate sentence.</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>1)<span style="white-space:pre">, Filter object is an array: use-(void) filterusingpredicate: (nspredicate *) predicate; Filter on mutable arrays to filter out non-conforming conditions in a mutable array</span> <span style="white-space:pre">. </span> <span style="white-space:pre">-(nsarray *) filteredarrayusingpredicate: (nspredicate *) predicate; Filter on immutable groups to return the eligible elements into a new array<br></span>2)<span style="white-space:pre">, a single object to determine the filter use:-(BOOL) evaluatewithobject: (id) object; Sends the method to the predicate object, which is the filtered Object. Common Yu Hezheng are used in conjunction with Expressions. </span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>Below are three ways to demonstrate the top of Each.</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span>a),<span style="white-space:pre">-(void) filterusingpredicate: (nspredicate *) predicate; Filtering on a mutable array</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">1. Create 10 person objects and add the array nsmutablearray *muarray = [nsmutablearray array]; for (int i = 0; i <10; i++) {person *person = [person personwithname:[nsstring stringwithformat:@ ' zhang%d ', i] age: i]; [muarray addobject:person]; } NSLog (@ "muarray =%@", muarray);</pre></pre><pre name="code" class="objc"><pre name="code" class="objc">2, the definition predicate sentence: The age attribute is more than 3 years old, note: The key path cannot use single quotation mark to cause, otherwise will error nspredicate *predicate = [nspredicate predicatewithformat:@ "ages > 3 "];</pre></pre><pre name="code" class="objc"><pre name="code" class="objc">3, filter the variable array [muarray filterusingpredicate:predicate]; NSLog (@ "variable Array filtered results%@", muarray);</pre></pre><span style="white-space:pre"><span style="white-space:pre"></span></span>We can see that the result of the printing is:<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="plain"><pre name="code" class="plain">2015-06-17 11:58:23.126 nspredicate[3183:277528] variable array filtered results ( "name = zhang4, age = 4", "name = zhang5, age = 5", <c 2 /> ' name = zhang6, age = 6 ', ' name = zhang7, age = 7 ', ' name = zhang8, age = 8 ', ' name = zhang9, age = 9 '</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>B),-(nsarray *) filteredarrayusingpredicate: (nspredicate *) predicate; Filters for immutable groups, and returns the elements that match the criteria into a new array.<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(nsarray *) filteredarrayusingpredicate: (nspredicate *) predicate; Filter on immutable groups to return nsarray *beforefilterarray = [nsarray arraywitharray: (nsarray *) muarray] , which is a new array of eligible elements Nsarray *afterfilterarray = [beforefilterarray filteredarrayusingpredicate:predicate]; NSLog (@ "non-variable Group Filter result%@", afterfilterarray);</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>We can see that the printing results are:<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="plain"><pre name="code" class="plain">2015-06-17 11:58:23.126 nspredicate[3183:277528] non-variable Group Filter results ( "name = zhang4, age = 4", "name = zhang5, age = 5", <c2 /> ' name = zhang6, age = 6 ', ' name = zhang7, age = 7 ', ' name = zhang8, age = 8 ', ' name = zhang9, age = 9 '</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>C),-(BOOL) evaluatewithobject: (id) object; Filter on each element of an array<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">Nsmutablearray *matchobjarray = [nsmutablearray array]; For (person *item in Muarray) { //A, determine whether the condition //judgment statement is Met: evaluatewithobject, the filter is met to return Yes if ([predicate Evaluatewithobject:item]) { [matchobjarray addobject:item]; } } NSLog (@ "filter-eligible Objects are:%@", matchobjarray);</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>The filter results are:<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><pre name="code" class="objc"><pre name="code" class="objc">2015-06-17 11:58:23.125 nspredicate[3183:277528] The object that meets the filter criteria is: ( "name = zhang4, age = 4", "name = zhang5, age = 5", ' name = zhang6, age = 6 ', ' name = zhang7, age = 7 ', ' name = zhang8, age = 8 ', ' name = zhang9, Age = 9 ")</pre></pre><br><span style="white-space:pre"><span style="white-space:pre"></span></span>Summary: the above three methods can execute predicate phrases, if it is to filter the array elements, we use:<span style="white-space:pre"><span style="white-space:pre">-(void) filterusingpredicate: (nspredicate *) predicate,-(nsarray *) filteredarrayusingpredicate: (nspredicate *) predicate, more convenient, If it is only for one object to judge, use</span></span>-(BOOL) evaluatewithobject: (id) object; More Convenient.<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span><br><br><br><br><br></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="white-space:pre"></span><br></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(61,29,129)"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(61,29,129)"><br></p></p><br><br><br><p><p>iOS development, verb (nspredicate) usage: (i) basic usage</p></p></span>

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.