Re: Sorting with NSNull's ???
Re: Sorting with NSNull's ???
- Subject: Re: Sorting with NSNull's ???
- From: Nir Soffer <email@hidden>
- Date: Sun, 24 Sep 2006 02:00:21 +0300
What if you use @"" instead of [NSNull null] ?
On Sep 23, 2006, at 10:37 PM, John Webb wrote:
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
Best Regards,
Nir Soffer
_______________________________________________
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