Objective Sea

Sailing on the Objective-C sea, see?
Written by Cory, from Davander Mobile.
@corydmc on twitter, or email me objectivesea@davander.com

Get the maximum value in an NSSet using KVC »

You can get the maximum value in an NSSet full of NSNumbers using Key-Value-Coding like so:

NSSet *set = [NSSet setWithArray:@[ @1,@2,@3,@4,@5 ]];
NSNumber *maxValue = [set valueForKeyPath:@"@max.self"];

Value of maxValue is now @5.

It took me ages to find the correct KVC syntax for this. Source is linked below, and has a bunch of other cool uses of KVC.


(This was prompted by a recent question on StackOverflow.)

Key-value coding is one of my more favorite things about Cocoa (up there with NSPredicate). Basically, it allows you to access properties of objects by name (ie, as a string) rather than invoking a method (er… sending a message)…