It appears that if I declare an Objective-C protocol in a project but no classes in the project/target actually adopt the protocol, the runtime doesn't know about the protocol at runtime. (i.e. NSProtocolFromString(@"MyProtocol")
returns NULL
) However, if I add a minimal "dummy" class, like this:
@interface Dummy : NSObject < MyProtocol >
@end
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprotocol"
@implementation Dummy
@end
#pragma clang diagnostic pop
...the runtime will know about the existence of the protocol at runtime.
Is there a more elegant/less-hacky way to ensure that an otherwise-unadopted protocol will be "known" at runtime?
Try
@protocol(MyProtocol)
instead ofNSProtocolFromString(@"MyProtocol")
. I found it near the bottom of this page http://nshipster.com/at-compiler-directives/. Does that help answer the question?