Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
- Subject: Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
- From: "Adam R. Maxwell" <email@hidden>
- Date: Fri, 06 Jun 2008 15:53:40 -0700
On Jun 6, 2008, at 2:51 PM, Ken Thomases wrote:
On Jun 6, 2008, at 4:16 PM, Adam R. Maxwell wrote:
On Friday, June 06, 2008, at 02:07PM, "George Stuart" <email@hidden
> wrote:
The suggestion of using a keyPath of @"self" presents another
question:
1) I assume NSSortDescriptor is using valueForKeyPath:, which in
turns calls valueForKey: some number of times, to get the object for
the keyPath I specified. Is it defined behavior that sending an
object valueForKey:@"self" will return the object itself?
I always use self as the key path in cases like this, since -
[NSObject self] is documented to return the receiver.
And, to complete the picture, the documentation for valueForKey:
describes how that method obtains the value for the key. The first
step in its search for key @"self" would be to look for the method
named "self".
It has the advantage of working with objects other than strings.
Well, so does -description. ;) It just gives a different thing.
That is, -self always returns the receiver, while -description only
returns the receiver for an NSString, but always returns something
(a string) that may be useful for a given purpose (possibly even
sorting).
Sure, the combination of KVC, sort descriptors, and categories allows
a lot of creativity: -description can be used for a comparison if
NSString implements the method you need, or if you add it as an
NSString category. So for an array of NSNumbers you could use -
description and a -numericCompare: method that uses NSNumericSearch.
In my opinion, it makes more sense to use -self and -compare: for
NSDate/NSNumber.
--
Adam
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden