Objective-C compiler is leaving out a protocol definition

ios, llvm, objective-c, objective-c-runtime, protocols

Solution

You can force the compiler to include the protocol by making a dummy method that's not called that uses it. I've done this before:

void preserveProtocolFromBeingTrimmed()
{
     (void)@protocol(BrightnessProtocol);
}

I see that Apple uses this in their FxBrightness plug-in from the FxPlug SDK.

Problem

I'm writing a couple classes that make use of the Objective-C runtime library. This includes retrieving Protocol definitions at runtime based on their name. However, it appears that Protocols that are not explicitly adopted by a class or referenced in code using @protocol(ProtocolName) are excluded from compilation and not available at runtime. Example: ``` @protocol MyProtocol <NSObject> -(void)doSomething; @end ``` //Somewhere else in code ``` Protocol *protocol = NSProtocolFromString(@"MyProtocol"); // ^ value of "protocol" will be nil when I run the application! ``` //However, if I use do the following: ``` Protocol *whyDoIHaveToDoThis = @protocol(MyProtocol); Protocol *protocol = NSProtocolFromString(@"MyProtocol"); // ^ value of "protocol" will now be a pointer as expected when I run the application! ``` Does anyone know why this is, and even better, how to force the compiler to include Protocol definitions that are not in use at compile time, but which I will later want available at runtime?

Original source

Related problems