(I do not know the answer from the top of my head). 1 minute of the search query helped me.)
You can do this using the protocol_copyMethodDescriptionList() function, which is part of the Objective-C runtime library (libobjc). The second argument to this function is a boolean flag indicating whether methods that need to be copied in the protocol are needed. Thus, if the method is in the list returned by this function (called using the appropriate arguments), then this is the required method.
SEL sctr = @selector(isThisMethod:requiredIn:theProtocol:); struct objc_method_description *methods; unsigned int nMethods; methods = protocol_copyMethodDescriptionList( objc_getProtocol("MyProtocolName"),
So itβs possible, but itβs a bit overkill, and as I mentioned in my comment on your question, you should not check that the method is optional or required, you should check the instance that responds to a specific selector.
Edit: yes, instead of copying the whole universe, I should have read further in the documentation. As Andrew Madsen noted, this can be summarized in a few lines:
struct objc_method_description method; method = protocol_getMethodDescription( objc_getProtocol("MyProtocolName"), // or @protocol(MyProtocolName) @selector(isThisSelector:required:) YES, // required? YES // instance method? ); if (method.name != NULL) { // required } else { // optional }
user529758
source share