iphone - KVC vs fast enumeration -
which of following faster , why?
cgfloat sum = 0; (uiview *v in self.subviews) sum += v.frame.size.height;
or
cgfloat sum = [[self.subviews valueforkeypath:@"@sum.frame.size.height"] floatvalue];
really, lot of how elegant (or clever) language comes down how avoids loops. for, while; fast enumeration expressions drag. no matter how sugar-coat them, loops block of code simpler describe in natural language.
"get me average salary of of employees in array",
double totalsalary = 0.0; (employee *employee in employees) { totalsalary += [employee.salary doublevalue]; } double averagesalary = totalsalary / [employees count];
versus...
fortunately, key-value coding gives more concise--almost ruby-like--way this:
[employees valueforkeypath:@"@avg.salary"];
kvc collection operators allows actions performed on collection using key path notation in valueforkeypath:.
any time see @ in key path, denotes particular aggregate function result can returned or chained, other key path.
fast enumeration faster kvc.
hope helps you.
Comments
Post a Comment