Even though Interface Builder is aware of a MyClass
, I get an error when starting the application.
This happens when MyClass
is part of a library, and does not happen if I compile the class directly in the application target.
Even though Interface Builder is aware of a MyClass
, I get an error when starting the application.
This happens when MyClass
is part of a library, and does not happen if I compile the class directly in the application target.
Despite the "Unknown class MyClass in Interface Builder file." error printed at runtime, this issue has nothing to do with Interface Builder, but rather with the linker, which is not linking a class because no code uses it directly.
When the .nib data (compiled from the .xib) is loaded at runtime, MyClass
is referenced using a string, but the linker doesn't analyze code functionality, just code existence, so it doesn't know that. Since no other source files references that class, the linker optimizes it out of existence when making the executable. So when Apple's code tries to load such a class, it can't find the code associated with it, and prints the warning.
The hack I've been using is to add an empty static routine like:
+(void)_keepAtLinkTime;
which does nothing, but that I call once, such as:
int main( int argc, char** argv )
{
[MyClass _keepAtLinkTime];
// Your code.
}
This forces the linker to keep the whole class, and the error disappears.
Of course, you can call this in any location of your code. I guess it could even be in unreachable code. The idea is to fool the linker into thinking that MyClass
is used somewhere so that it isn't so aggressive in optimizing it out.
You can fix this by setting the ObjC linker flag. See the following forum post for details:
Not only in project settings, but in Target setting also u have to add -all_load -ObjC flags..