开发者

How to refer to a method of a class?

开发者 https://www.devze.com 2023-02-12 04:24 出处:网络
I have a problem when using qsort。 qsort(ArrayToSort, size_a, size_b, FunctionPointer); IfFunctionPointer is declared as int (* FunctionPointer)(); then it\'s fine.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消