Encoding Type for Protocol Method

I am trying to get a signature - either an NSMethodSignature object, or at least a type encoding string - for a method declared in the protocol.

Requesting a Protocol object is not possible, because a) it does not implement methodSignatureForSelector: and b) (as Kevin noted below), it is deprecated.

The protocol_getMethodDescription runtime function returns a struct objc_method_description that is not described elsewhere in the documents. This is in the public header, however - <objc / runtime.h> :

 struct objc_method_description { SEL name; char *types; }; 

It seems reasonable to assume that the types string there will be the same signature encoding string used elsewhere, such as expected with +[NSMethodSignature signatureWithObjCTypes:] , and indeed, it looks correct.

What I cannot keep track of is the real, testable connection between this string and the type coding process.

I can’t think what else would happen, but do I have every reason to rely on this types line to be valid for interacting with other objects / functions in the same work ? Please note that I myself do not write coding strings or do not expect them to have the specified format or value. I only want to transfer them from one part of the runtime to another, i.e. Get the encoding string from the protocol and) use it to generate the NSMethodSignature object, if it is not otherwise available, and, possibly, b) compare it with the version of NSInvocation created at run time (i.e. in -forwardInvocation: .

+4
source share
1 answer

Using Protocol as an object is deprecated. If you check the <objc/Protocol.h> header, you will see that almost everything on it is either not available in OBJC-2, or is deprecated like OS X 10.5. You can use protocol_getMethodDescription() , as you said, and pull out the types field. I am not sure if it is officially registered that it is a type of encoding method, but it really is what it is.

+5
source

Source: https://habr.com/ru/post/1390114/


All Articles