• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSSortDescriptor and Block
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSSortDescriptor and Block


  • Subject: NSSortDescriptor and Block
  • From: Jochen Moeller <email@hidden>
  • Date: Wed, 24 Mar 2010 09:51:49 +0100

Hello List,

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?

Thanks for comments,
Jochen Moeller


Here the Listing (Foundation Tool with one class):

// MyObject.h, SortTest
#import <Foundation/Foundation.h>
@interface MyObject : NSObject {
 double _rx;
}
- (id) initWithX:(double)x;
- (double)rx;
- (void)setRx:(double)r;
@end

// MyObject.m, SortTest
#import "MyObject.h"
@implementation MyObject
- (id) init {
 return [ self initWithX:0.0 ];
}
- (id) initWithX:(double)x { // mit Ortsvektor R-2D und idx
 self = [ super init ];
 if (self != nil) {
   _rx = x;
 }
 return self;
}
- (NSString *)description { return [ NSString stringWithFormat:@"%.1f", _rx ]; }
- (double)rx { return _rx; }
- (void)setRx:(double)r { _rx = r; }
@end

// SortTest.m, Mac OS X 10.6.2 (10C540)
#import <Foundation/Foundation.h>
#import "MyObject.h"

int main (int argc, const char * argv[]) {
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 double x[] = { 5.0, 1.3, 4.9, 2.2, 3.8 }; // 5 items
 NSInteger j;
 MyObject *obj;
 NSMutableArray *array = [[ NSMutableArray alloc ] init ]; // Array to sort

 for ( j = 0; j < 5; j++ ) {
   obj = [[ MyObject alloc ] initWithX:x[j]];
   [ array addObject:obj ];
   [ obj release ];
 }
 NSLog(@"unsorted: %@", [array description]);

 // Sort array with -sortDescriptorWithKey:ascending:selector:
 NSSortDescriptor *descr1 = [ NSSortDescriptor sortDescriptorWithKey:@"rx" ascending:YES
     selector:@selector(compare:) ];
 NSArray *descrArray1 = [ NSArray arrayWithObject:descr1 ];
 NSArray *sortedArray1 = [ array sortedArrayUsingDescriptors:descrArray1 ];
 NSLog(@"sorted 1: %@", [sortedArray1 description]);

 // Sort array with Block
 NSArray *sortedArray2 = [ array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
   if ([obj1 rx] < [obj2 rx]) { return NSOrderedAscending; }
   else if ([obj1 rx] > [obj2 rx]) { return NSOrderedDescending; }
   return NSOrderedSame;
 }];
 NSLog(@"sorted 2: %@", [sortedArray2 description]);

 // 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;
 }];
 NSLog(@"after descr3");
 NSArray *descrArray3 = [ NSArray arrayWithObject:descr3 ];
 NSLog(@"after descrArray3");
 NSArray *sortedArray3 = [ array sortedArrayUsingDescriptors:descrArray3 ]; // Exception is here
 NSLog(@"sorted 3: %@", [sortedArray3 description]);

 [ array release ];
 [pool drain];
 return 0;
}

_______________________________________________

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

  • Prev by Date: Re: NSNotificationCenter and a modal window - a problem [solved]
  • Next by Date: Fwd: NSTreeController, NSBrowser image setting
  • Previous by thread: Re: Allowing incoming connections.
  • Next by thread: re: NSSortDescriptor and Block
  • Index(es):
    • Date
    • Thread