开发者

Objective c: All selectors of instance during runtime [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

List selectors 开发者_StackOverflow社区for obj-c object

Does anybody know how to get all selectors that a instance respond to during runtime in objective C?


As answered here, #import < objc/runtime.h > and use class_copyMethodList().


In general, this is not possible. "The selectors an instance responds to" may be an infinite set. For example, it is possible to implement a class that is sent Roman numerals as messages and returns the corresponding integer value. If you want to know the precise set of instance methods implemented by a class at a given time (which is a different question), you can just use the Objective-C runtime functions to get a class's instance method list and walk up the class tree to find the ones it inherits from superclasses. Again, though, these are two totally different things. A class might have a method for a message that it chooses not to respond to and it might respond to messages for which it does not have a directly corresponding method.


dapptrace (Dtrace) is your friend.

on the man page (man dapptrace):

dapptrace prints details on user and library function calls

dapptrace is written for the Dtrace scripting language (D). This means you can adjust dapptrace or pull ideas from it's script file to do many things. For instance:

wait for myFunctionWhichCreatesSpecialObject to be called. Store the object address that it returns (the special object). Print out any selectors invoked on that object.

You can also invoke dtrace directly to write simple single-line spells. I'll let you go search for those.


During runtime you would use

the class method "+ (BOOL)instancesRespondToSelector:(SEL)aSelector"

provided you know the selectors you want to check on.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜