Re: NSSortDescriptor not working with array of custom classes
Re: NSSortDescriptor not working with array of custom classes
- Subject: Re: NSSortDescriptor not working with array of custom classes
- From: Ken Tozier <email@hidden>
- Date: Sun, 4 Nov 2007 18:25:24 -0500
On Nov 4, 2007, at 6:05 PM, Shawn Erickson wrote:
Can you post a complete code sample that shows the problem you are
reporting. We are having to guess at what your class(es) involved
really look like. You can simplify it down to some thing that shows
the issue you having.
That last post was pretty much where the problem is, but here's a
couple of other methods to flesh it in
// page view class is defined like so:
@interface PMPageView : NSView
{
PMPageModel *model;
BOOL isEven,
active,
selected;
KIntegerField *pageNumberField;
NSColor *fillColor;
int pageNumber,
clickCount;
unsigned int selectionModifiers;
NSRect intFrame;
}
/* if uncommented, sorting works but it just seems silly to have to
do this
- (id) valueForKeyPath:(id) inKey
{
if ([inKey isEqualToString: @"model.pageNumber"])
return [model pageNumber];
else
return [super valueForKeyPath: inKey];
}
*/
// page model class is defined like so:
@interface PMPageModel : NSObject
{
PMPublicationModel *publication;
NSMutableDictionary *pageRecord;
PHPInvocation *invocation;
NSString *color;
}
// User types in an NSTextFiled to change the number of a page.
Custom page matrix superview receives a notification in it's
"observeValueForKeyPath" method
- (void) observeValueForKeyPath:(NSString *) inKeyPath
ofObject:(id) inObject
change:(NSDictionary *) inChange
context:(void *) inContext
{
if ([inKeyPath isEqualToString: @"pageRecord.page_number"])
{
[self handlePageNumberChange];
}
}
// observeValueForKeyPath calls handler for the page number change
- (void) handlePageNumberChange
{
[self sortPageViews];
// update the records mod date (for observers)
[self setRecordsModified: recordsModified + 1];
}
// handlePageNumberChange calls "sortPageViews" method (Note:
sortPageViews is used by other methods so it's been factored into its
own method)
- (void) sortPageViews
{
NSLog(@"pages before sortine: %@, class: %@", pages, [pages class]);
NSMutableArray *desc = [[NSMutableArray alloc] init];
// these all print valid values to the run log
NSLog(@"first page: %@", [pages objectAtIndex: 0]);
NSLog(@"direct call page view accessor: %i", [[pages objectAtIndex:
0] pageNumber]);
NSLog(@"direct call model accessor: %@", [[[pages objectAtIndex: 0]
model] pageNumber]);
NSLog(@"direct call model raw accessor: %@", [[[[pages
objectAtIndex: 0] model] valueForKeyPath: @"pageRecord"]
objectForKey: @"page_number"]);
NSLog(@"direct call model raw accessor 2: %@", [[[pages
objectAtIndex: 0] model] valueForKeyPath: @"pageRecord.page_number"]);
// these all print null
NSLog(@"page view accessor: %@", [[pages objectAtIndex: 0]
valueForKeyPath: @"pageNumber"]);
NSLog(@"model accessor: %@", [[pages objectAtIndex: 0]
valueForKeyPath: @"model.pageNumber"]);
NSLog(@"model raw accessor: %@", [[pages objectAtIndex: 0]
valueForKeyPath: @"model.pageRecord.page_number"]);
// all of these fail unless I define "
[desc addObject: [[NSSortDescriptor alloc] initWithKey:
@"pageNumber" ascending: YES]];
//[desc addObject: [[NSSortDescriptor alloc] initWithKey:
@"model.pageNumber" ascending: YES]];
//[desc addObject: [[NSSortDescriptor alloc] initWithKey:
@"model.pageRecord.page_number" ascending: YES]];
//[desc addObject: [[NSSortDescriptor alloc] initWithKey:
@"model.pageNumber" ascending: YES]];
NSArray *sortedCells = [pages sortedArrayUsingDescriptors: desc];
NSLog(@"pages after sortine: %@", sortedCells);
[pages release];
pages = [[sortedCells mutableCopy] retain];
[self updateCellFramesWithWidth: [self frame].size.width
forceUpdate: YES];
}
Ken
_______________________________________________
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