Sorting with NSNull's ???
Sorting with NSNull's ???
- Subject: Sorting with NSNull's ???
- From: John Webb <email@hidden>
- Date: Sat, 23 Sep 2006 14:37:42 -0500
I've had a very difficult time getting sorting in TableViews where
some columns contain NSNull values working. I'm hoping I'm missing
something obvious, and that there's a easier solution than the "hack"
I came up with.
My TableView displays info pulled from a DB and I use the NSNull
singleton to represent null DB values. I must maintain/allow null
values for when I write any edited data back to the DB.
Initially, in IB I set the TableColumn Sort Key to the appropriate
datasource field name, and set the Sort Selector to 'compare:'. If a
column had no NSNull values, sorting worked perfectly. However is
there were any NSNull values, I would get an exception (eg. NSNull
selector compare: no recognized; selector getCType not recognized,
etc.) I was extremely surprised that compare: (or NSMutableArray
sortUsingDescriptors: , which is the actual method I'm using in my
data source implementation ) didn't check for or recognize NSNull
values. It seems like sorting arrays with NSNull values would be
extremely common and handled transparently by Cocoa.
So how I end up getting around this problem is that I created a
Category on NSObject (since that's NSNull's superclass, not, say,
NSValue ) to add a new method, ncompare:, which would check for
NSNull. Here's what the code looks like:
@protocol Comparable
- (NSComparisonResult) compare:(id) obj ;
@end
@interface NSObject (NCompareMethod)
- (NSComparisonResult) ncompare:(id) obj ;
@end
@implementation NSObject (NCompareMethod)
-(NSComparisonResult) ncompare:(id) obj
{
id null = [NSNull null];
if ( self == obj ){
{
return NSOrderedSame;
}
else if ( self == null )
{
return NSOrderedAscending;
}
else if ( obj == null )
{
return NSOrderedDescending;
}
// no NSNulls involved, so do normal
compare:
return [ (<Comparable>) self compare:obj ];
}
@end
There's got to be a simpler way, right ?! What am I missing?
TIA,
John
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden