Problems with Sorting a NSArray Containing Simple NSString O


Now, at the end of chapter 8, I could solve the two challenges. However, the section beforehand the challenges gives me a real headache …
It explains how to sort by using - tableView:sortDescriptorsDidChange: which helps a lot for the second challenge. But I have a problem sorting the array of my ToDos application as mentioned in the section.

The problem is that the NSTableColumn needs a “Sort Key”, which is personName and - expectedRaise in RaiseMan. However, for the ToDo Application I used a NSArray with NSStrings.
How can I sort this array of strings? If I do not use a “Sort Key”, the method - tableView:sortDescriptorsDidChange: is never invoked. As soon as I use a key, I of course get an exception:

How can I sort simple arrays? Is there any magic key (I’ve already tried “*”, but this gives the same exception …)?

I’ve added the listings to clarify my question.


#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (weak) IBOutlet NSTextField *inputItemField;
@property (weak) IBOutlet NSTableView *toDoTableView;

@property (nonatomic, strong) NSMutableArray *items;

- (IBAction)createNewItem:(id)sender;



#import "AppDelegate.h"

@implementation AppDelegate

@synthesize toDoTableView;
@synthesize inputItemField;
@synthesize items;

- (id)init
    self = [super init];
    if (self) [self setItems:[NSMutableArray array]];
    return self;

- (IBAction)createNewItem:(id)sender
    [items addObject:[inputItemField stringValue]];
    [toDoTableView reloadData];
    [inputItemField setStringValue:@""];

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
    return [items count];

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
    return [items objectAtIndex];

- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
    NSLog(@"- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors");
    NSArray *newDescriptors = [aTableView sortDescriptors];
    [items sortUsingDescriptors:newDescriptors];
    [toDoTableView reloadData];