@interface Foo : NSObject {
}
- (Bar)bar;
在运行时,给定[Foo class],我们如何找出Foo所有方法的返回类型?
我研究Objective-C运行时API已经有一段时间了;据我所知,没有办法获得方法或属性的返回类型(即Class)。这在API的任何反思中似乎都是一个严重的遗漏。
您可以使用Objective-C运行时函数:
Method class_getClassMethod(Class aClass, SEL aSelector)
void method_getReturnType(Method method, char *dst, size_t dst_len)
Objective-C运行时参考
首先,使用class_getClassMethod
获取方法对象
Method m = class_getClassMethod( [ SomeClass class ], @selector( someMethod ) );
然后,使用method_getReturnType
:询问返回类型
char ret[ 256 ];
method_getReturnType( m, ret, 256 );
NSLog( @"Return type: %s", ret );
您可以告诉基本信息,比如它是什么C类型,但无法区分返回类型上的各种实例类型(或类)。
Objective-C从来没有被设计过,也从来没有打算有这样的反思。主要是因为这样做会大大增加可执行文件(所有元数据)的大小,并且支持所有C类型(比如C++类型)的全参数/返回类型元数据内省将非常复杂。
在将C++对象传递给Objective-C方法时,与方法参数化相关的元数据已经测量到比生成的代码大几个数量级(字面意思是单个方法调用的元数据的MB)。
参数/返回类型元数据形式中相对简单类型的示例:
{CGRect={CGPoint=dd}{CGSize=dd}}96@0:8@16{CGRect={CGPoint=dd}{CGSize=dd}}24{CGRect={CGPoint=dd}{CGSize=dd}}56q88
WebKit的一个特别惊人的例子是:
^{Document=^^?^^?i^{ContainerNode}{Weak=^{JSValue}}^{Document}^}Node}^^{RenderObject}I^{Node}^{Node}^{TreeScope}{DocumentOrderedMap={HashMap,WTF::HashTraits,WTF::HashTraits
={HashTable,WTF::PairFirstExtractor,WTF::PtrHash,WTF::PairHashTraits,WTF::哈希特征,WTF::哈希特征=^{pair}iiii}}{HashCountedSet,WTF::HashTraits={HashMap,WTF::HashTraits,WTF:HashTraits={HashTable,WTF::PairFirstExtractor,WTF::PtrHash,WTF::PairHashTraits,WTF::哈希特征,WTF::哈希特征=^{pair}iiii}}}}{DocumentOrderedMap={HashMap,WTF::HashTraits,WTF:HashTraits={HashTable,WTF::PairFirstExtractor,WTF::PtrHash,WTF::PairHashTraits,WTF::哈希特征,WTF::哈希特征=^{pair}iiii}}{HashCountedSet,WTF::HashTraits={HashMap,WTF::HashTraits,WTF:HashTraits={HashTable,WTF::PairFirstExtractor,WTF::PtrHash,WTF::PairHashTraits,WTF::哈希特征,WTF::哈希特征=^{pair}iiii}}}}我^^?{RefPtr=^{SecurityOrigin}}{HashSet,WTF::HashTraits={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{MessagePort}iiii}}{HashMap,WTF::HashTraits,WTF:HashTraits={HashTable,WTF::PairFirstExtractor,WTF::PtrHash,WTF::PairHashTraits,WTF::HashTraits>,WTF::HashTraits=^{pair}iiii}}BB{HashMap,WTF::HashTraits,WTF:HashTraits={HashTable,WTF::PairFirstExtractor>,WTF:IntHash,WTF:PairHashTraits,WTF::HashTraits>,WTF::HashTraits=^{pair}iiii}}B{OwnPtr,0ul>=^{Vector,0ul>}}{RefPtr=^{DatabaseThread}}Bi{OwnPtr=^{CSSTYLESelector}BB{RefPtr=^{CSSPrimitiveValueCache}}}^{Frame}{OwnPtr=^}CachedResourceLoader}}}RefPtr=^{StringImpl}}={String={RefPtr=^{StringImpl}}},0ul>=^{Vector,0ul>}}BBBBiB{Color=IB}{RefPtr=^{Node}}={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{NodeIterator}iiii}}{HashSet,WTF::HashTraits={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{Range}iiii}}S{RefPtr=^{StyleSheetList}}}{ListHashSet={HashTable,WTF::ListHashSetNode,WTF::IdentityExtractor*>,WTF::ListHashSetNodeHashFunctions,WTF::HashTraits*>,WTF::HashTraits*>=^^{ListHashSetNode}iiii}^{ListHashSetNode}^{List HashSet Node}{OwnPtr=^{ListHashSetNodeAllocator}}{ListHashSet={HashTable,WTF::ListHashSetNode,WTF::IdentityExtractor*>,WTF::ListHashSetNodeHashFunctions,WTF::HashTraits*>,WTF::HashTraits*>=^^{ListHashSetNode}iiii}^{ListHashSetNode}^{List HashSet Node}{OwnPtr=^{ListHashSetNodeAllocator}}{ListHashSet={HashTable,WTF::ListHashSetNode,WTF::IdentityExtractor*>,WTF::ListHashSetNodeHashFunctions,WTF::HashTraits*>,WTF::HashTraits*>=^^{ListHashSetNode}iiii}^{ListHashSetNode}^{List HashSet Node}{OwnPtr=^{ListHashSetNodeAllocator}}{HashMap,WebCore::FormElementKeyHash,WebCorer::FormElement KeyHashTraits,WTF::HashTraits>={HashTable,WTF::PairFirstExtractor>,WebCore::FormElementKeyHash,WTF:PairHashTraits>,WebCore::FormElementKeyHashTraits>=^{对}iiii}}{Color=IB}{Color=IB}{Color=IB}{String={RefPtr=^{StringImpl}}{}^{AXObjectCache}{OwnPtr=^{DocumentMarkerController}}{{RefPtr=^{SerializedScriptValue}}dBi{OwnPtr=^{ScriptRunner}}}{Own Ptr=^2{TransformSource}},WTF::哈希特征,WTF::HashTraits>=^{HashMap,WTF:,HashTraits,WTF:HashTraits}}}{FixedArray=[10{CollectionCache=Q^{Element}IIi{HashMap,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits*>={HashTable,std::pair*>,WTF=:PairFirstExtractor*>,WTF::HashTraits**,WTF::HashTraits=^{pair*>}iiii}}{HashMap,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits*>={HashTable,std::pair*>,WTF=:PairFirstExtractor*>,WTF::HashTraits**,WTF::HashTraits=^{pair*>}iiii}}BB}]}{FixedArray,WTF::哈希特征,WTF::哈希特征,2ul>=[2{HashMap,WTF::HashTraits,WTF:HashTraits={HashTable,WTF::PairFirstExtractor,WTF::PtrHash,WTF::PairHashTraits,WTF::哈希特征,WTF::哈希特征=^{pair}iiii}}]}{RefPtr=^{XPathEvaluator}}{OwnPtr=^{SVGDocumentExtensions}}
={哈希表,WTF::PairFirstExtractor>,WTF:StringHash,WTF:PairHashTraits,WTF::HashTraits>,WTF::HashTraits=^{对}iiii}}BB[1{IconURL=i{KURL={String={RefPtr=^{StringImpl}}}b1b1iiiiiiii}}]{HashSet,WTF::HashTraits={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{Element}iiii}}{HashSet,WTF::HashTraits={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{Element}iiii}}{HashSet,WTF::HashTraits={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{Element}iiii}}{HashMap,WTF::HashTraits={HashTable,WTF::PairFirstExtractor,WTF::CaseFoldingHash,WTF::PairHashTraits,WTF::HashTraits>,WTF::HashTraits=^{pair}iiii}}BBBBBBB{RefPtr=^{EventQueue}}={HashTable,WTF::PtrHash,WTF:HashTraits,WTF:HashTraits=^^{MediaCanStartListener}iiii}}{QualifiedName=^{QualfiedNameImpl}}}B{RefPtr=^{Element}^{RenderFullScreen}{Timer=^^?ddiI^{Document}{{?=^q}}{Deque,0ul>=QQ{VectorBuffer,0ul>=^{RefPtr}q}}f}BB{DocumentTiming=ddddd}{RefPtr=^{MediaQueryMatcher}}}