Unknown class in interface builder file

I added a UIView xib file using the root class of MyView .

I created it in the wrong place and so moved it in the project. Same project just a different folder/group.

I then had a problem when running saying.

Unknown Class MyView in Interface Builder file

I couldn’t work out what was wrong so I have now deleted the files both from the project and from the directory.

I’ve done a search using SublimeText2 for the string "MyView" and it doesn’t exist anywhere in the project.

I’ve reset the simulator, cleaned the project and the build folder and deleted derived data.

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.

46 Answers 46

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.

By default, Objective-C targets will have -all_load -ObjC flags 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.

Читайте также:  Pioneer vsx 418 цена

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 _keepAtLinkTime method. 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 MyClass is used somewhere so that it isn’t so aggressive in optimizing it out.

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.

Я добавил файл xib UIView с использованием корневого класса MyView .

Я создал его в неправильном месте и так переместил его в проект. Тот же проект просто другой папки/группы.

У меня возникла проблема при запуске сообщения.

Unknown Class MyView in Interface Builder file

Я не мог понять, что не так, поэтому я удалил файлы как из проекта, так и из каталога.

Я выполнил поиск с использованием SublimeText2 для строки "MyView" и не существует нигде в проекте.

Я использовал reset симулятор, очистил проект и папку сборки и удалил полученные данные.

По-прежнему возникает ошибка.

Любые идеи, что я могу сделать сейчас?

ОК, поэтому я удалил все ссылки на класс, вызывающий проблему, но все равно ничего.

Читайте также:  Dwa 131 driver windows 7

Однако у меня также был подкласс UIViewController под названием MyViewController . Это не загружалось ни в какой IB файл, поэтому я не знаю, как это вызвало проблему.

Я изменил имя на MyOtherViewController .

Build — Run — работает.

Я не знаю, как и почему, но теперь это работает.

Rate this post

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *