Key-Value Coding
Hi Guys!
Today, I'm going to work with Key-Value Coding. To explain why we need key-Value Coding, I've an example for you
To calculate an average salary from n number of employees, You'll use loops and some coding stuff to add all employees salary and then Calculate average divided by No. of employees.
Fortunately, Apple Provides Key-value Coding gives us a much more concise--almost Ruby-like--way to do this:
Collection Operators fall into one of three different categories, according to the kind of value they return:
[products valueForKeyPath:@"@count"]; // 4
[products valueForKeyPath:@"@sum.price"]; // 3526.00
[products valueForKeyPath:@"@avg.price"]; // 881.50
[products valueForKeyPath:@"@max.price"]; // 1699.00
[products valueForKeyPath:@"@min.launchedOn"]; // June 11, 2012
Object Operators
Example
[inventory valueForKeyPath:@"@unionOfObjects.name"]; // "iPhone 5", "iPhone 5", "iPhone 5", "iPad Mini", "MacBook Pro", "MacBook Pro"
[inventory valueForKeyPath:@"@distinctUnionOfObjects.name"]; // "iPhone 5", "iPad Mini", "MacBook Pro"
Hi Guys!
Today, I'm going to work with Key-Value Coding. To explain why we need key-Value Coding, I've an example for you
To calculate an average salary from n number of employees, You'll use loops and some coding stuff to add all employees salary and then Calculate average divided by No. of employees.
double totalSalary = 0.0;
for (Employee *employee in employees) {
totalSalary += [employee.salary doubleValue];
}
double averageSalary = totalSalary / [employees count];
for (Employee *employee in employees) {
totalSalary += [employee.salary doubleValue];
}
double averageSalary = totalSalary / [employees count];
Fortunately, Apple Provides Key-value Coding gives us a much more concise--almost Ruby-like--way to do this:
[employees valueForKeyPath:@"@avg.salary"];
Key-Value Operators used with valueForKeyPath and @ denotes a particular aggregate function
- Simple Collection Operators return strings, numbers, or dates, depending on the operator.
- Object Operators return an array.
- Array and Set Operators return an array or set, depending on the operator.
- @count: Returns the number of objects in the collection as an NSNumber.
- @sum: Converts each object in the collection to a double, computes the sum, and returns the sum as an NSNumber.
- @avg: Takes the double value of each object in the collection, and returns the average value as an NSNumber.
- @max: Determines the maximum value using compare:. Objects must support comparison with one another for this to work.
- @min: Same as @max, but returns the minimum value in the collection.
[products valueForKeyPath:@"@count"]; // 4
[products valueForKeyPath:@"@sum.price"]; // 3526.00
[products valueForKeyPath:@"@avg.price"]; // 881.50
[products valueForKeyPath:@"@max.price"]; // 1699.00
[products valueForKeyPath:@"@min.launchedOn"]; // June 11, 2012
Object Operators
- @unionOfObjects / @distinctUnionOfObjects: Returns an array of the objects in the property specified in the key path to the right of the operator. @distinctUnionOfObjects removes duplicates, whereas @unionOfObjects does not.
Example
[inventory valueForKeyPath:@"@unionOfObjects.name"]; // "iPhone 5", "iPhone 5", "iPhone 5", "iPad Mini", "MacBook Pro", "MacBook Pro"
[inventory valueForKeyPath:@"@distinctUnionOfObjects.name"]; // "iPhone 5", "iPad Mini", "MacBook Pro"
No comments:
Post a Comment