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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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,
MyClassis 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.By default, Objective-C targets will have
-all_load -ObjCflags set by default, which will keep all of the symbols. But I had started with a C++ target, and didn’t have that. Nevertheless, I found a way around this, which keeps the linker aggressive.The hack I was originally using was to add an empty static routine like:
which does nothing, but that I would call once, such as:
This would force the linker to keep the whole class, and the error disappears.
As jlstrecker pointed out in the comments, we do not really need to add a
_keepAtLinkTimemethod. Simply calling an existing one, such as:does the trick (as long as you derive from an
NSObject).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
MyClassis used somewhere so that it isn’t so aggressive in optimizing it out.Xcode 6.3.2 & Swift 1.2
Swift definition of view. Be sure to override
init(coder aDecoder: NSCoder). Objective-C definition of view controller. And, a nib in a pear tree.Add Module Name to Nib details inspector where you pick your class.