• 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: Chris Giordano <email@hidden>
  • Date: Fri, 23 Apr 2004 10:40:55 -0400

Aurilien,

A thought -- take it for what it is worth. (Might not be much -- you have been warned.)

On Apr 23, 2004, at 4:42 AM, 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...

so basically i want my autosortedArray to be based on selector. You
would be able to init the autosortedArray with a Selector OR a C
function. At init, the class would transform the C function to the
matching selector...

i have looked at NSInvocation etc.. but it is incomprehensible, at
least for me !

could someone give me an example to convert one of those C function
above to a simple selector, something that could be used like this :
[firstElt ascendingFirstName:secondElt];

thanks !


Rather than trying to convert the functions to a method, what about the idea of creating a sort of wrapper method that calls the appropriate function. You'd store the selected function somewhere accessible (wherever you'd be storing the selector selected by setSelector:), and have the wrapper method that did something like the following:

// this would be the variable that stored the function to be used
int (*compareFunction)(id, id);

- (int) compareUsingSelectedFunction:(id) secondElt
{
return compareFunction(self, secondElt);
}

or, if the comparison function is stored somewhere other than in the class whose objects you're comparing, you could pass it into your compare method:

- (int) compareTo:(id) secondElt usingFunction:(int (*)(int, int)) compareFunction
{
return compareFunction(self, secondElt);
}

You could then have either a selector chosen to use for the comparisons, or you could choose a function (so long as it met the prototype) by setting the function variable and then using the selector for compareUsingSelectedFunction: or compareTo:usingFunction: as the comparison selector.

chris
_______________________________________________
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: Behavior bind:toObject:withKeyPath:options:
  • Next by Date: app icons, strings, and drops
  • Previous by thread: C function to a Selector ?
  • Next by thread: Re: C function to a Selector ?
  • Index(es):
    • Date
    • Thread