• 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: NSSortDescriptor and Block
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSSortDescriptor and Block


  • Subject: Re: NSSortDescriptor and Block
  • From: Ben Trumbull <email@hidden>
  • Date: Wed, 24 Mar 2010 15:17:41 -0700

Well, if you need a nil keypath you probably shouldn't be using sort descriptors.  The primary value of sort descriptors are (a) the KVC caching and (b) an OO representation of a sort criteria.

The standard -sortedArrayUsingComparator: is for blocks like the one you wrote that are completely free form.

- Ben


On Mar 24, 2010, at 3:55 AM, Jochen Moeller wrote:

> Hello Ben,
>
> thanks very much for your answer. Your idea with the nil keypath works!
>
> So changing the line in question to:
>
> NSSortDescriptor *descr3 = [ NSSortDescriptor sortDescriptorWithKey:nil ascending:YES
>   comparator:^NSComparisonResult(id obj1, id obj2) {
>
> is sorting the array without claims.
>
> The syntax of that method is a bit confusing. When using a selector the key is required, and when using a comparator it is not.
>
> Thanks again,
> Jochen Moeller
>
>
> Am 24.03.2010 um 11:12 schrieb Ben Trumbull:
>
>>> while experimenting with sorting methods I got a strange error message with
>>> -sortDescriptorWithKey:ascending:comparator:.
>>>
>>> In the following listing (as simple as possible) an Array can be sorted with
>>> -sortedArrayUsingDescriptors: with a selector, and
>>> -sortedArrayUsingComparator:
>>>
>>> But -sortedArrayUsingDescriptors: with a comparator results in
>>> Error: -[NSCFNumber rx]: unrecognized selector.
>>>
>>> So the method expects the key "rx" not in my class as it should but in the NSNumber class.
>>> A bug? Or is something wrong in my approach?
>>
>> The arguments to the Block with the NSSortDescriptor are the results of calling valueForKeyPath, not the original objects in the array.  NSSortDescriptor has already pulled the values for the keypaths out and caches them.  So your using the key @"rx" with that block is like:
>>
>> [[obj1 valueForKeyPath:@"rx"] rx]
>>
>> If you don't want NSSortDescriptors KVC functionality, you can just use -sortedArrayUsingComparator:.  You can also try a nil keypath, although I'm not certain that works.
>>
>> - Ben
>>
>>> // Sort array with -sortDescriptorWithKey:ascending:comparator:
>>> // Error: -[NSCFNumber rx]: unrecognized selector
>>> NSSortDescriptor *descr3 = [ NSSortDescriptor sortDescriptorWithKey:@"rx" ascending:YES
>>>   comparator:^NSComparisonResult(id obj1, id obj2) {
>>> if ([obj1 rx] < [obj2 rx]) { return NSOrderedAscending; }
>>> else if ([obj1 rx] > [obj2 rx]) { return NSOrderedDescending; }
>>> return NSOrderedSame;
>>> }];
>>
>>
>


- Ben



_______________________________________________

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

References: 
 >re: NSSortDescriptor and Block (From: Ben Trumbull <email@hidden>)
 >Re: NSSortDescriptor and Block (From: Jochen Moeller <email@hidden>)

  • Prev by Date: Re: Can I add my own characters to strings in text fields?
  • Next by Date: Re: Can I add my own characters to strings in text fields?
  • Previous by thread: Re: NSSortDescriptor and Block
  • Next by thread: How to only get color change messages when the user changes color?
  • Index(es):
    • Date
    • Thread