• 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: C function to a Selector ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C function to a Selector ?


  • Subject: Re: C function to a Selector ?
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Wed, 28 Apr 2004 00:13:21 +0200

At 10:42 Uhr +0200 23.04.2004, Aurilien Hugeli wrote:
hi !

i have *a lots* of C functions defined as this :
static int _ascendingFirstNameOrder(id firstElt,id secondElt);
static int _ascendingLastNameOrder(id firstElt,id secondElt);
...

I m coding a sort of autosortedArray using binary search to sort items
as they are inserted in the array.
by default the selector will be @selector(compare:), but can be changed
using a sort of setSelector:

I can't honestly convert all the previous C function to selector by
rewritting them, it would be *VERY* long...

Have you thought about simply allowing functions as well? The NSArray methods for sorting come in two flavors: selector-based, and function-based. But note that the functions also take a void* as their third parameter, which the client can use to pass in additional data.

If you want to save yourself from writing separate code for these two cases, implement selector-based comparisons by way of the function-based ones by providing a default function:

int compareBySelector( id a, id b, void* userData )
{
SEL theSelector = (SEL) userData;

return [a performSelector: theSelector withObject: b];
}

That way, your home-grown class would fit right in with existing Cocoa stuff, and you could share your comparison functions between your sorted array and NSArray if needed.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >C function to a Selector ? (From: Aurélien Hugelé <email@hidden>)

  • Prev by Date: Re: Is KVO really as complicated as I think?
  • Next by Date: Re: [Q] am I allowed to call NSTableDataSource informal protocol methods myself?
  • Previous by thread: Re: C function to a Selector ?
  • Next by thread: Problems with DO+rendezvous
  • Index(es):
    • Date
    • Thread