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开发者_如何学Python MyMethod is my own class method,
int MyMethod(const void *,const void *);
Please advise me. Thanks a lot!
Don't use a class method for a comparison. Just declare a regular C function and pass that.
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.
精彩评论