Well, if you need a nil keypath you probably shouldn't be using sort descriptors. The primary value of sort descriptors are (a) the KVC caching and (b) an OO representation of a sort criteria.
The standard -sortedArrayUsingComparator: is for blocks like the one you wrote that are completely free form. - Ben On Mar 24, 2010, at 3:55 AM, Jochen Moeller wrote: > Hello Ben, > > thanks very much for your answer. Your idea with the nil keypath works! > > So changing the line in question to: > > NSSortDescriptor *descr3 = [ NSSortDescriptor sortDescriptorWithKey:nil > ascending:YES > comparator:^NSComparisonResult(id obj1, id obj2) { > > is sorting the array without claims. > > The syntax of that method is a bit confusing. When using a selector the key > is required, and when using a comparator it is not. > > Thanks again, > Jochen Moeller > > > Am 24.03.2010 um 11:12 schrieb Ben Trumbull: > >>> while experimenting with sorting methods I got a strange error message with >>> -sortDescriptorWithKey:ascending:comparator:. >>> >>> In the following listing (as simple as possible) an Array can be sorted with >>> -sortedArrayUsingDescriptors: with a selector, and >>> -sortedArrayUsingComparator: >>> >>> But -sortedArrayUsingDescriptors: with a comparator results in >>> Error: -[NSCFNumber rx]: unrecognized selector. >>> >>> So the method expects the key "rx" not in my class as it should but in the >>> NSNumber class. >>> A bug? Or is something wrong in my approach? >> >> The arguments to the Block with the NSSortDescriptor are the results of >> calling valueForKeyPath, not the original objects in the array. >> NSSortDescriptor has already pulled the values for the keypaths out and >> caches them. So your using the key @"rx" with that block is like: >> >> [[obj1 valueForKeyPath:@"rx"] rx] >> >> If you don't want NSSortDescriptors KVC functionality, you can just use >> -sortedArrayUsingComparator:. You can also try a nil keypath, although I'm >> not certain that works. >> >> - Ben >> >>> // Sort array with -sortDescriptorWithKey:ascending:comparator: >>> // Error: -[NSCFNumber rx]: unrecognized selector >>> NSSortDescriptor *descr3 = [ NSSortDescriptor sortDescriptorWithKey:@"rx" >>> ascending:YES >>> comparator:^NSComparisonResult(id obj1, id obj2) { >>> if ([obj1 rx] < [obj2 rx]) { return NSOrderedAscending; } >>> else if ([obj1 rx] > [obj2 rx]) { return NSOrderedDescending; } >>> return NSOrderedSame; >>> }]; >> >> > - Ben _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com