[英]Type encoding string for protocol method
I'm trying to get a signature -- either an NSMethodSignature
object or at least the type encoding string -- for a method declared in a protocol. 我正在尝试为协议中声明的方法获取签名 -
NSMethodSignature
对象或至少是类型编码字符串。
Asking the Protocol
object itself isn't possible, since a) it doesn't implement methodSignatureForSelector:
, and b) (as noted by Kevin below) it's deprecated. 询问
Protocol
对象本身是不可能的,因为a)它没有实现methodSignatureForSelector:
和b)(如下面的Kevin所述)它已被弃用。
The runtime function protocol_getMethodDescription
returns a struct objc_method_description
, which isn't described anywhere in the docs. 运行时函数
protocol_getMethodDescription
返回struct objc_method_description
,在文档中的任何位置都没有描述。 It's in a public header, though -- <objc/runtime.h> : 它位于公共标题中,但是 - <objc / runtime.h> :
struct objc_method_description {
SEL name;
char *types;
};
It seems reasonable to assume that the types
string in there is going to be the same kind of signature encoding string used elsewhere, such as that expected by +[NSMethodSignature signatureWithObjCTypes:]
, and indeed, it looks correct. 假设那里的
types
字符串将是与其他地方使用的相同类型的签名编码字符串似乎是合理的,例如+[NSMethodSignature signatureWithObjCTypes:]
所期望的那样,实际上,它看起来是正确的。
What I can't track down is an actual, verifiable connection between that string and the type encoding process. 我无法追踪的是该字符串与类型编码过程之间的实际可验证连接。
I can't think what else it would be, but still, do I have any justification for relying on this types
string to be valid for interaction with other objects/functions on the same runtime ? 我想不出它会是什么,但是,我是否还有理由依赖这个
types
字符串来有效地与同一运行时的其他对象/函数进行交互? Note that I'm not writing encoding strings myself or expecting them to have a given format or value -- I only want to pass them from one part of the runtime/framework to another, ie, retrieve an encoding string from a protocol and a) use it to generate an NSMethodSignature
object if one isn't otherwise available, and possibly b) compare it to that of a runtime-generated NSInvocation
(ie, in -forwardInvocation:
). 请注意,我不是自己编写字符串或期望它们具有给定的格式或值 - 我只想将它们从运行时/框架的一部分传递给另一部分,即从协议中检索编码字符串和)如果其他方法不可用,则使用它来生成
NSMethodSignature
对象,并且可能b)将其与运行时生成的NSInvocation
(即in- -forwardInvocation:
。
Using Protocol
as an object is deprecated. 不建议使用
Protocol
作为对象。 If you check the header <objc/Protocol.h>
you'll see that pretty much everything on it is either not available in OBJC-2 or is deprecated as of OS X 10.5. 如果你检查标题
<objc/Protocol.h>
你会发现它上面的所有东西要么在OBJC-2中不可用,要么在OS X 10.5中被弃用。 What you can do is use protocol_getMethodDescription()
, as you suggested, and pull out the types
field. 你可以做的是使用你建议的
protocol_getMethodDescription()
,并拉出types
字段。 I'm not sure if it's actually officially documented that this is the type encoding of the method, but that is indeed what it is. 我不确定它是否已经正式记录,这是该方法的类型编码,但事实确实如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.