Small talk about the set operator of KeyPath in KVC

Source: Internet
Author: User

Because the knowledge point is relatively simple, there is no longer a large set of principles, straight into the subject.

The set operators in KVC have the following three categories:

1. Simple set Operator: @avg, @sum, @max, @min, @count ( can only be used in collection objects, object properties must be numeric types )

   2. Object operators:

               @ unionofobjects: Returns an array of the values of the specified property, without the weight

@distinctUnionOfObjects: returns an array of values after the specified property is de-weighed

3. array/Collective operators: is similar to the object operator, except that it works in a NSArray NSSet set of components.

@unionOfArrays: Returns an array that consists of the elements of each sub-array, not the weight

@distinctUnionOfArrays: Returns an array that consists of the elements of each sub-array, to remove the weight

@distinctUnionOfSets: similar to @distinctunionofarrays, except that it expects a nsset that contains the Nsset object and returns a Nsset object. Because the collection cannot have duplicate values, only the distinct operation.

On the code:

Scenario: There is a person class with the name and age two properties. Instantiate 5 objects to find their average age, sum of age, maximum age and minimum age.

Person class header file:

1 #import<Foundation/Foundation.h>2 3 @interfacePerson:nsobject4 5 /**6 name7  */8@property (nonatomic,copy) NSString *name;9 Ten /** One Age A  */ - @property (nonatomic,assign) Nsinteger age; -  the /** - Custom Constructors -  - @param Name + @param age -  + @return object that returns person A  */ at-(Instancetype) Initwithname: (NSString *) name Andage: (Nsinteger) age; -  - @end

. m files for Person:

1 #import "Person.h"2 3 @implementation Person4 5-(Instancetype) Initwithname: (NSString *) name Andage: (nsinteger) age{6     if(self =[Super Init]) {7_name =name;8_age =Age ;9     }Ten     returnSelf ; One } A  - @end

The controller invokes the Person object implementation function:

1 @interfaceViewcontroller ()2 3 @end4 5 @implementationViewcontroller6 7- (void) Viewdidload {8 [Super Viewdidload];9     TenPerson *P1 = [[Person alloc] Initwithname:@"xiaoming"Andage:Ten]; OnePerson *P2 = [[Person alloc] Initwithname:@"xiaoming"Andage: the]; APerson *P3 = [[Person alloc] Initwithname:@"Xiaohong"Andage: -]; -Person *P4 = [[Person alloc] Initwithname:@"Xiaoli"Andage: -]; -Person *P5 = [[Person alloc] Initwithname:@"Xiaoli"Andage: -]; the      -     //Simple set operators cannot be used in a single object, so there will be an error -     //int res = [[P1 valueforkeypath:@] @sum. Age "] integervalue]; -      +Nsarray *perarray =@[p1, p2, P3, P4, P5]; -      +     //Simple Set Operators ANsinteger avg = [[Perarray valueforkeypath:@"@avg. Age"] IntegerValue]; atNsinteger sum = [[Perarray valueforkeypath:@"@sum. Age"] IntegerValue]; -Nsinteger max = [[Perarray valueforkeypath:@"@max. Age"] IntegerValue]; -Nsinteger min = [[Perarray valueforkeypath:@"@min. Age"] IntegerValue]; -      -     //Count: Take the number of elements in the array, the following 3 kinds of notation are equivalent -Nsinteger count = [[Perarray valueforkeypath:@"@count. Age"] IntegerValue]; inNsinteger count1 = [[Perarray valueforkeypath:@"@count"] IntegerValue]; -Nsinteger Count2 =Perarray.count; to      +      -     //Object operators: Working with array objects the     //@unionOfObjects: Returns an array of the values of the specified property, without the weight *     //@distinctUnionOfObjects: Returns an array of values after the specified property is de-weighed $nsarray<nsstring *> *namearray = [Perarray valueforkeypath:@"@unionOfObjects. Name"];Panax Notoginsengnsarray<nsstring *> *namearray2 = [Perarray valueforkeypath:@"@distinctUnionOfObjects. Name"]; -      the     //array/Collective operators: Operations on collections composed of Nsarray and Nsset +     //unionofarrays: Returns an array whose values consist of the elements of each sub-array, not the weight A     //distinctunionofarrays: Returns an array whose values consist of the elements of each sub-array to the     //distinctunionofsets: Similar to distinctunionofarrays, except that it expects a nsset that contains Nsset objects and returns a Nsset object. Because a collection cannot contain duplicate values, it has only distinct operations.  +Person *P1 = [[Person alloc] Initwithname:@"PP1"Andage:Ten]; -Person *P2 = [[Person alloc] Initwithname:@"PP1"Andage: -]; $Person *P3 = [[Person alloc] Initwithname:@"PP2"Andage: -]; $Person *P4 = [[Person alloc] Initwithname:@"PP3"Andage: +]; -Person *P5 = [[Person alloc] Initwithname:@"PP3"Andage: -]; -Nsarray *perarray2 =@[p1, P2, P3, P4, P5]; the      -Nsarray *arr1 = [@[perarray,perarray2] Valueforkeypath:@"@unionOfArrays. Name"];WuyiNsarray *ARR2 = [@[perarray,perarray2] Valueforkeypath:@"@distinctUnionOfArrays. Name"]; the } -  Wu @end

Small talk about the set operator of KeyPath in KVC

Related Article

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.