IamSailing
IamSailing

Reputation: 1

How to refer to a method of a class?

I have a problem when using qsort。

qsort(ArrayToSort, size_a, size_b, FunctionPointer);

If FunctionPointer is declared as int (* FunctionPointer)(); then it's fine.

If FunctionPointer is declared as FunctionPointer = @selector(MyMethod); then I have run time error of BAD_ACCESS。

Here MyMethod is my own class method,

int MyMethod(const void *,const void *);

Please advise me. Thanks a lot!

Upvotes: 0

Views: 101

Answers (2)

kubi
kubi

Reputation: 49364

The problem is that you're trying to mix C code with Obj-C code. @selector(someMethod:) returns a SEL which is neither a C function nor an Obj-C method. What you'll want to do is either follow @yan's suggestion and use a C function for you comparison or (my suggestion), don't bother using qsort at all, just use the NSArray methods to do your sorting.

Upvotes: 1

yan
yan

Reputation: 20982

Don't use a class method for a comparison. Just declare a regular C function and pass that.

Upvotes: 1

Related Questions