• 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
Re: Sorting with NSNull's ???
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Sorting with NSNull's ??? (From: John Webb <email@hidden>)

  • Prev by Date: Re: SQL Development
  • Next by Date: Re: Sorting with NSNull's ???
  • Previous by thread: Sorting with NSNull's ???
  • Next by thread: Re: Sorting with NSNull's ???
  • Index(es):
    • Date
    • Thread