Check if a method exists

前端 未结 5 2041
半阙折子戏
半阙折子戏 2020-12-13 01:58

Is there any way I can test if a method exists in Objective-C?

I\'m trying to add a guard to see if my object has the method before calling it.

5条回答
  •  感情败类
    2020-12-13 02:26

    There is also the static message instancesRespondToSelector:(SEL)selector You would call it like this:

    [MyClass instancesRespondToSelector:@selector(someMethod:withParams:)]
    

    or like this:

    [[myObject class] instancesRespondToSelector:@selector(someMethod:withParams:)]
    

    This may be useful if you would like to call one constructor or another one depending on this (I mean, before having the instance itself).

提交回复
热议问题